Catching an Error : Error Exceptions « Development « JavaScript DHTML

Catching an Error

<TITLE>Catch that error!</TITLE>
function catchError(errString) { 
   try { 
      try { 
         if (errString == 42) 
            throw new Error (42, "errString is 42 !"); 
            throw new Error (0, "errString is NOT 42 !"); 
      catch(e) { 
         if (e.number == 42) 
            return (e.description + " Got this one!"); 
            throw e; // re-throw the error 
    catch (e){ 
       return(e.description + " This one not handled here!"); 
Catch that error! 
<FORM name="theForm">
<TR><TD colspan=2> We can handle 42: </TD><TD><INPUT type=text name=errText size=40 value="42"></TD></TR>
<TR><TD colspan=2></TD>
<TD><INPUT type=button name=btnThrow value="Catch it!" onClick="alert(catchError(document.theForm.errText.value));">


Related examples in the same category

1.Catching the 'Object Expected' Error
2.Throwing an Error
3.Controlling Script Errors
4.An Exception Handling Example
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