Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of error handling in Javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you what are the relevant knowledge points about error handling methods in Javascript, which are detailed in content and clear in logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

We know that since the browser we are using will not throw an exception exception of type Error, if we catch an Error exception in our development, we can make sure that the exception is thrown by our code and not by the browser we are using.

1. Use _ window.onerror to specify the error handling function

When there are errors in development, onerror will be callback. If there are multiple script errors in a JavaScript block in development, after the first error is triggered, the script after the current JavaScript block will be automatically ignored by Drop and will not be executed. Let's take a look at the following code:

Test _ window.onerror = function (message, url, line) {alert ("Error.\ nMessage:" + message + "\ nUrl:" + url + "\ nLine:" + line) return true;} test (); test ()

In the above code, there is only the first test () in each block; an error will trigger our _ window.onerror callback, and the subsequent JavaScript will be ignored, and the code that supports onerror in img is as follows:

< img src="pic.gif" onerror = "_javascript:alert("An error occurred.");"/>

Because onerror is an object supported by the browser, it is up to the browser to decide whether it can be used or not, not the DOM standard

2. Use 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 this article entitled "what are the methods of error handling in Javascript?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report