Try... catch is a very practical and safe method for capturing errors. The usage is as follows. Example reference:Http://www.cnblogs.com/newblue/archive/2006/12/28/606100.html <HTML> <Head> <title> try... catch... finally </title> <Body> <SCRIPT type = "text/JavaScript"> // <! [CDATA [ // In JavaScript, try... catch can be used for exception handling. For example: Try { Foo. Bar (); } Catch (e ){ Alert (E. Name + ":" + E. Message ); } // Currently, the possible system exceptions include the following 6 types:
// Evalerror: raised when an error occurs executing code in eval () // Rangeerror: raised when a numeric variable or parameter is outside of its valid range // Referenceerror: raised when de-referencing an invalid reference // Syntaxerror: raised when a syntax error occurs while parsing code in eval () // Typeerror: raised when a variable or parameter is not a valid type // Urierror: raised when encodeuri () or decodeuri () are passed invalid Parameters
// The preceding six exception objects are inherited from error objects. They both support the following two constructor methods:
// New error (); // New error ("exception information "); // The method for manually throwing an exception is as follows: Try { Throw new error ("whoops! "); } Catch (e ){ Alert (E. Name + ":" + E. Message ); }
// To determine the type of exception information, you can make a judgment in catch: Try { Foo. Bar (); } Catch (e ){ If (E instanceof evalerror ){ Alert (E. Name + ":" + E. Message ); } Else if (E instanceof rangeerror ){ Alert (E. Name + ":" + E. Message ); } // Etc } // Error has the following attributes: // Description: Error description (ie only ). // Filename: the file name with an error (available only for Mozilla ). // Linenumber: number of lines in the error (available only for Mozilla ). // Message: error message (Description in IE) // Name: Error type. // Number: Error Code (ie only available ). // Stack: Error stack information similar to the stack trace in Java (available only by Mozilla ). // To better understand the error information, you can change the catch part to the following format: Try { Foo. Bar (); } Catch (e ){ If (browsertype! = Browser_ie ){ Alert ( "Name:" + E. Name + "/Nmessage:" + E. Message + "/Nlinenumber:" + E. linenumber + "/Nfilename:" + E. filename + "/Nstack:" + E. Stack ); } Else { Alert ( "Name:" + E. Name + "/Nerrornumber:" + (E. Number & 0 xFFFF) + "/Nmessage:" + E. Message "); } }
// The throw command in javascript can actually throw any object, and we can accept this object in catch. For example: Try { Throw new date (); // throw the current time object } Catch (e ){ Alert (E. tolocalestring (); // display the current time in local format } //]> </SCRIPT> </Body> </Html> // Exception handling test in different browsers <HTML> <Head> <title> try... catch... finally </title> <Body> <SCRIPT type = "text/JavaScript"> // <! [CDATA [ // Exception handling test in different browsers // Firefox3 // Message: foo is not defined; filename:File: // D:/test/test_apply_call.html; Linenumber: 10; Stack: @ file: // D:/test/test_apply_call.html: 10; Name: referenceerror; // IE7 // Name: typeerror; message: 'foo' undefined; number:-2146823279; Description: 'foo' undefined; Try { Foo. Bar (); } Catch (e ){ VaR S = ''; For (I in E) { S + = I + ":" + E [I] + ";"; } Document. Write (s ); } //]> </SCRIPT> </Body> </Html> |