Javascript error handling methods

Source: Internet
Author: User

1. Use window. onerror to specify the error handling function.
When an error occurs, onerror is called back. When multiple script errors exist in a JavaScript block, the script after the first error is triggered (callback) is automatically dropped and not executed.
For example:
Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Test </title>
<Script type = "text/javascript">
Window. onerror = function (message, url, line)
{
Alert ("Error. \ nMessage:" + message + "\ nUrl:" + url + "\ nLine:" + line)
Return true;
}
</Script>
</Head>
<Body>
<Script type = "text/javascript">
Test ();
Test ();
Test ();
Test ();
</Script>
<Script type = "text/javascript">
Test ();
Test ();
Test ();
Test ();
</Script>
</Body>
</Html>

In the preceding example, only the first test () in each block is generated. When the window. onerror callback is triggered, the subsequent Javascript will be ignored. Img also supports onerror . Onerror is an object supported by the browser. The browser determines whether it can be used, not the DOM standard.

2. Use try catch throw in Javascript to handle exceptions.
Javascript supports try catch throw. exceptions defined in Javascript:
(1) EvalError: An error occurs in the eval () function.
(2) RangeError: A number value is greater then or less then the number that can be represented in Javascript (Number. MAX_VALUE and Number. MIN_VAKUE ).
(3) ReferenceError: An illegal reference is used.
(4) SyntaxError: A syntax error occus inside of an eval () function call. All other syntax error are reorted by the browser and cannot be handled with a try... catch statement.
(5) TypeError. A variables type is unexpected. 6. URIError. An error ocuurs in the encodeURI () or the decodeURI () function.
For example:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function CreateError ()
{
Throw new Error ("Created error by custom .");
}
Try
{
// Throw a error from a function just want to see the call stack in firefox.
CreateError ();
}
Catch (error)
{
Var errorMsg = ("Message:" + error. message + "\ n ");
If (typeof (error. stack )! = Undefined)
{
// FF
ErrorMsg + = ("Line Number:" + error. lineNumber + "\ n ");
ErrorMsg + = ("File Name:" + error. fileName + "\ n ");
ErrorMsg + = ("Stack Trace: \ n" + error. stack + "\ n ");
}
Else
{
// IE
ErrorMsg + = ("Description:" + error. description + "\ n ");
ErrorMsg + = ("Number:" + error. number + "\ n ");
}
Alert (errorMsg );
}
Finally
{
// Alert ("End try catch. message from finally block .");
}
</Script>


Error. message is a property supported by both IE and FireFox.
IE supports the description and number attributes.
FF supports fileName lineNumber and stack attributes.
Javascript is a weak language.
Therefore, the catch part can only be caught once. A language like C # can write multiple catch statements to catch different types of exceptions.
However, you can use instanceof ErrorType to implement similar functions.
For example:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Try
{// Syntax Error
// Eval ("alert ");

// Custom Error
Throw new Error ("An error occured .");
}
Catch (error)
{
If (error instanceof SyntaxError)
{
Alert ("Syntax Error ");
}
Else if (error instanceof EvalError)
{
Alert ("Eval Error ");
}
Else if (error instanceof RangeError)
{
Alert ("Range Error ");
}
Else if (error instanceof ReferenceError)
{
Alert ("Reference Error ");
}
Else if (error instanceof TypeError)
{
Alert ("Type Error ");
}
Else if (error instanceof Error)
{
Alert ("Custon Error ");
}
Alert (error. message );
}
</Script>


Note: the browser will not throw an exception of the Error type, so if an exception of the Error type is caught, you can confirm that this exception is thrown by the user code, not by the browser.
Javascript assert ()
Copy codeThe Code is as follows:
Function assert (bCondition, sErrorMsg ){
If (! BCondition ){
Alert (sErrorMsg );
Throw new Error (sErrorMsg );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.