Exceptions and their handling

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.