Use return in the Try...catch statement in the Aardio function.

Source: Internet
Author: User
Tags try catch

try{error    ("wrong message");    Use error to throw an error    //If the error function is in a try statement, the program will not error, but will jump out of the Try statement}catch (e) {    ///If the Try statement is followed by a catch statement, you can catch this error message E}

The above is the grammar of Try...catch;

The try statement tries to execute a block of statements and then exits the try statement block instead of interrupting the Aardio program if an error is encountered.
If you use a Catch statement block, you can catch the exception (the catch statement block is optional).

The above is a passage in the Aardio user manual.

Perhaps in the process of using try catch, we often want to return an error directly when an error occurs.

As an example:

= function () {try{        error ("1");    }     Catch (e) {        return  e;     }}console.log (Test ()); Console.pause (true);

We return this error message e If an error occurs during test execution.

But it's wrong to write like this. orzing~

A null was returned. (That is, the test function has no return value at all)

Later, after many tests, it was found that using return in a try or catch simply jumped out of the curly brace of a try or catch, so the function was not returned.

To return the catch to the error message E, you can use the following method:

= function () {    var  err;     Try {        error ("1");    }     Catch (e) {        = e;    }     return err;} Console.log (Test ()); Console.pause (true);

Define a local variable in the function body err, you can assign the error message E to the ERR variable in the catch statement;

Then return in the body of the function, so that you can return the catch to the error message!

Use return in the Try...catch statement in the Aardio function.

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.