In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.