JavaScript catch exception

Source: Internet
Author: User

Catch exceptions in javascript:
Everyone is not thinking debugging JavaScript very painful, I also feel.
We'll use it if we can find a good way to help us debug JavaScript.
In fact, JavaScript can also catch exceptions, such as:

<div id= ' BBB ' style= "Display:none" >asdf</div>
<script>
function A ()
... {
Try
... {
var AAA = document.getElementById (' BBB ');
alert (Aaa.innertext);
}
catch (E)
... {
Alert (' ERROR ' + E.message + ' occurs in ' + E.linenumber + ' line ');
}
}
A ();
</script>

<div id= ' BBB ' style= "Display:none" >asdf</div>
<script>
function A ()
... {
Try
... {
var AAA = document.getElementById (' Sakdhglas ');
alert (Aaa.innertext);
}
catch (E)
... {
Alert (' ERROR ' + E.message + ' occurs in ' + E.linenumber + ' line ');
}
}
A ();
</script>

will be an error, JS exception e In addition to E.message and E.linenumber there is an important attribute E.name

E.name error type,
E.message the details of the error.

Error.name six values correspond to the information:
1. The use of Evalerror:eval () is inconsistent with the definition
2. Rangeerror: Value out of bounds
3. Referenceerror: Illegal or unrecognized reference value
4. SyntaxError: Syntax resolution error occurred
5. TypeError: Error of operand type
6. Improper use of urierror:uri processing function

So your JS code in the run to catch the exception code, even if the code error, will continue to run the code, will not stop, and the use of the exception to JS debugging also has some help.



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.