Today, mainly for the JS code in the process of running the exception and its processing method is simple to make the following summary:
# exception
# # Exception Concept
An exception is an error that occurs during the execution of a program.
After the exception in JS, the browser will give an error code, is the error message. Error messages consist of error types and error messages
# # How to handle exceptions
is to be able to continue execution after an exception occurs. The most unusual feature of the exception is that once the code has an exception, the code is no longer executed.
Common exceptions are in two main categories:
1. Diversity of operating environments
2. Syntax error, code error
# # # Try-catch syntax
That is, try to do this if error trapping errors occur
```
...
try {
Code that may be wrong
} catch (e) {
Code to handle the error
}
...
```
1. The code is working properly, if there is an error in try, the code behind the wrong statement in the try is no longer executed and jumps directly into the catch
2. Handling error messages in Catch
3. Then proceed to the following code:
4. If no error occurs in the try, then execute the following code without taking the catch directly
# # How to throw an exception
```
Throw Object
```
1. Throw is the syntax for throwing exceptions, followed by an object, the error message object
2. Typically this object is created using ' new error ' (' Error message ') '. Arbitrary objects are also supported.
```
function ShowMessage (msg) {
To display a piece of text, so I make a limit
if (typeof msg!== ' string ') {
throw new Error (' the passed parameter is not a string ');
}
It's normal.
Console.log (msg);
}
```
# # Supplement
1. The final structure of the Try-catch syntax is try-catch-finally
```
try {
Code that may be wrong
} catch (e) {
Execute if an error occurs
} finally {
End Try this code block before execution, i.e. last execution
}
```
2. Level transfer
```
Function F1 () {
F2 ();//F1 called caller, or call function, F2 called called, or called function
}
function F2 () {
F3 ();
}
Function F3 () {
throw new error (' Error ');
}
F1 ();
```
Exceptions and their handling