Several methods of Javascript error handling _javascript skills

Source: Internet
Author: User
Tags assert error handling finally block stack trace try catch
1. Use Window.onerror to specify error-handling functions.
When there is a mistake, onerror will be callback. When there are multiple script errors in a JavaScript block, the first error is triggered (the callback callback), and the script behind the current JavaScript block is ignored by the automatic drop and not executed.
Such as:
Copy Code code as follows:

<title>Test</title>
<script type= "Text/javascript" >
Window.onerror = function (message, URL, line)
{
Alert ("Error.\nmessage:" + message + "\nurl:" + URL + "\nline:" + line)
return true;
}
</script>
<body>
<script type= "Text/javascript" >
Test ();
Test ();
Test ();
Test ();
</script>
<script type= "Text/javascript" >
Test ();
Test ();
Test ();
Test ();
</script>
</body>

In the above example there will be only the first Test () in each block, and the error is generated. Triggers the window.onerror callback, and the following JavaScript is ignored. IMG also supports OnError < img src= "pic.gif" onerror = "Javascript:alert" ("An error occurred."); />. OnError is a browser-supported object. It is up to the browser to decide whether to use it, not the DOM standard.

2. Use a try catch throw in JavaScript to handle exceptions.
JavaScript supports the exceptions defined in the Try Catch Throw,javascript:
(1) Evalerror:an error occurs in the eval () function.
(2) RANGEERROR:A number value is greater then or less then the number this 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 the eval () function call. All of the other syntax error are reorted by the browser and cannot are 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.
Such as:
Copy Code code as follows:

<script type= "Text/javascript" >
function Createerror ()
{
throw new error ("Created error by Custom.");
}
Try
{
Throw a error from a function just want into 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 that both IE and Firefox support.
IE supports description and number properties.
FF supports the filename linenumber and Stack properties.
Because JavaScript is a weakly typed language.
So you can only catch once in the Catch section, and you can't write multiple catch,catch different types of exception like C #.
However, similar functions can be implemented in a instanceof errortype manner.
Such as:
Copy Code code as follows:

<script type= "Text/javascript" >
Try
{//syntax Error
Eval ("alert A");

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 does not throw an error type of exception exception, so if you catch an error type exception, you can determine that the exception was thrown by the user code, not by the browser.
The assert () of JavaScript
Copy Code code as follows:

function assert (bcondition, serrormsg) {
if (!bcondition) {
alert (serrormsg);
throw new Error (serrormsg);
}
}
Related Article

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.