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

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.