Mega Code Archive

 
Categories / JavaScript DHTML / Development
 

An Exception Handling Example

<HTML> <HEAD><TITLE>Exception Test</TITLE></HEAD> <SCRIPT LANGUAGE="JavaScript"><!-- function primeTest(n) {  document.write("Testing "+n+": ")  try {   if(n < 1 || n > 20)        throw "It's out of range"   for(var i = 2; i < n; ++i){      if(n % i == 0)          throw "It's divisible by " + i   }   document.writeln("It's prime.<BR>")  }  catch (exception) {   document.writeln(exception+".<BR>")  } } --></SCRIPT> <BODY> <P>This script only works with Internet Explorer 5, Navigator 6, or later browsers.</P> <SCRIPT LANGUAGE="JavaScript"><!-- for(i = 0; i <= 21; ++i) {  primeTest(i) } --></SCRIPT> </BODY> </HTML>