js017- error handling and debugging
Chapter Content
Understanding browser-Reported errors
Handling Errors
Debugging JS Code
17.2 Error Handling
17.2.1 Try-catch Statement
try{ Possible error code }catch (Error) { How to deal with the This error } |
1. Finally clause
This clause is optional in Try-catch, but once the finally clause is used, its code must be executed anyway:
function textfinally () { try{ Possible error code return 2; }catch (Error) { How to deal with the This error return 1; }finally{ return 0; } } |
The function returns only 0, and the function may return 2 only if finally is removed from the last face. |
2. Type of error
The following 7 types of errors: and their triggering scenarios
Error |
Base type |
Evalerror |
An exception that occurs when using the Eval () function is thrown |
Rangeerror |
triggered when the value exceeds the range |
Referenceerror |
Object Not found is triggered |
SyntaxError |
When we pass the wrong JS string into the eval () function is triggered |
TypeError |
An unexpected type is stored in a variable, or when a method that does not exist is accessed. |
Urierror |
When you use encodeURI () or decodeURI () and the URI is not properly formatted |
3. Reasonable use of Try-catch
17.2.2 Throw Error
The throw operator, which is used to throw custom errors at any time. When a throw is encountered, the code stops running immediately. The code inherits execution only if there is a try-catch catch to the thrown error.
1, the timing of throwing the wrong
2, throw error and use Try-catch
17.2.3 Error Events
Whenever an error occurs, either the browser-generated or not, the error event is triggered and an event handler is executed.
False is returned in the following event handler to organize the default behavior of the browser to report errors
Window.onerror = function (message, URL, line) { alert (message); return false; } |
The image also supports error events. The error event is triggered whenever the URL in the amount src attribute in the image cannot return an image format that can be recognized.
17.2.4 Strategies for handling Errors
17.2.5 Common types of Errors
The core of error handling is to first know what is going wrong in the code. In general, there are three types of errors to follow:
1, type conversion error;
Occurs when a language structure is used for an operator, or other data types that may be automatically converted to high. In use equal (= =) Unequal (! =) or if,for,while in a stream control statement with a non-Boolean value is the longest type conversion error occurred.
Use congruent and non-congruent operators to avoid type conversion errors caused by using equality and inequality operators
2, data type error;
3, communication error;
17.2.6 distinguishing between the name of the wrong and the non-fatal error
17.2.7 Log the error to the server
17.3 Commissioning Technology
17.3.1 logging messages to the console
Ie8,firefox,opera,chrome,safari, all have JS console, can be used to view JS error. And, in these browsers, I can output messages through the code to the console, and for Ie8,firefox,chrome,safari, the message can be written to the JS console via the console object, which has the following methods:
Error (Message) |
Logging error messages to the console |
Info (message) |
Logging informational messages to the console |
Log (message) |
Logging generic messages to the console |
Warn (message) |
Logging warning messages to the console |
17.3.2 log messages to the current page
Open a small area in the page to display small. This area is usually an element.
17.3.3 Throw Error
17.4 of Common IE Error
1. Termination of operation
2. Invalid characters
3. No members found
4. Unknown run-time error
5. Syntax error
6. The system cannot find the specified resource
Statement: the Blog by http://www.cnblogs.com/lal-fighting/Original published, without the author's permission, not to reprint plagiarism!!!
js017-Error Handling and debugging