How to use try catch throw in JavaScript to handle exceptions
This article mainly shows you "how to use try catch throw in JavaScript to handle exceptions", the content is simple and clear, hoping to help you solve your doubts, let the editor lead you to study and learn "how to use try catch throw in JavaScript to handle exceptions" this article.
Using try catch throw in JavaScript to handle exceptions
Define exceptions 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.
The code is as follows:
Function CreateError () {throw new Error ("Created error by custom.");} try {/ / throw an 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.");}
And in our code, Error.message is supported by both IE and FireFox, while IE supports description and number attributes.
FF supports fileName lineNumber and stack attributes. Because Javascript is a weakly typed language, you can only catch once in the catch section. Languages like C # cannot write multiple exception of different types of catch,catch. However, similar functions can be implemented in the instanceof ErrorType way. The code is as follows:
Try {/ / SyntaxError / / eval ("alert a"); / / Custom Error throw new Error ("An error occured.");} catch (error) {if (error instanceof SyntaxError) {alert ("SyntaxError");} else if (error instanceof EvalError) {alert ("EvalError");} else if (error instanceof RangeError) {alert ("RangeError");} else if (error instanceof ReferenceError) {alert ("ReferenceError") } else if (error instanceof TypeError) {alert ("TypeError");} else if (error instanceof Error) {alert ("Custon Error");} alert (error.message);}
For the assert () aspect of JavaScript, we can take a look at the following code:
Function assert (bCondition, sErrorMsg) {if (! bCondition) {alert (sErrorMsg); throw new Error (sErrorMsg);}} these are all the contents of the article "how to handle exceptions using try catch throw in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!