An Exception Handling Example : Error Exceptions « Development « JavaScript DHTML






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>
           
       








Related examples in the same category

1.Catching the 'Object Expected' Error
2.Throwing an Error
3.Catching an Error
4.Controlling Script Errors
5.Nested Exception Handling (This script only works with Internet Explorer 5, Navigator 6, or later browsers)
6.Using the onError Event Handler
7. Throwing String Exceptions
8.Throwing an Error Object Exception
9.A Custom Object Exception