1.Error objects
Represents an error that is created and thrown when an error occurs in the code run.
Create Error:
New Error ([Message],[filename],[linenumber])
Error type:
- Evalerror errors related to eval
- Internalerror JavaScript engine internal error, such as too many recursion
- Rangeerror a numeric variable or parameter exceeds the valid range
- Referenceerror Invalid reference
- SyntaxError syntax error
- TypeError variable or parameter is not a valid type
- Invalid argument passed to encodeURI () or decodeURI by Urierror
Error.prototype Main Properties
- Name: ' Error '
- Message error message
- Stack error Tracking
2. Handling Exceptions
The try Catch statement can handle exceptions. There are three types of structures:
- Try Catch
- Try finally
- Try Catch finally
The exception that occurs in try no longer executes the remaining code and executes the catch instead.
1 Try{//statements that need to be executed2Console.log (' Begin ');3 Throw NewError (' hoho! ');4 //throw new Error (' hoho! ', ' test.html ', ' 28 ');5Console.log (' after ');//will not be executed6 }7 Catch(ERR) {//statement executed when there is an exception in try8Console.error (Err.stack);//error:hoho! at test.html:299 //Console.log (err.name+ ":" +err.message); Error:hoho!Ten } One finally{//statements that execute regardless of whether or not an exception occurs AConsole.log (' End. ')); -}
When try is nested, the exception is captured one time by the nearest catch. A new exception can be thrown out of the catch. A return in inally will end the entire try and no longer handle the exception.
1 Try{2 Try{3 Throw NewError (' hoho! ');4}Catch(err) {5Console.error (' inner ', err.message);6 Throwerr;7}finally{8Console.log (' End ');9 }Ten}Catch(err) { OneConsole.error (' outer ', err.message); A } - //Output: - //End the //Inner hoho! - //outer hoho!
4. Monitor all exceptions
1 function // parameters take chrome as an example 2 3 Console.log (' Handling error ... '); 4 5 return true; // true to not prompt for error, flase prompt error 6 }78 //Allert (1);
JavaScript Errors and exceptions