JavaScript Errors and exceptions

Source: Internet
Author: User
Tags try catch

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

Related Article

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.