Exception handling of JavaScript
in Note with 0 comment
Exception handling of JavaScript
in Note with 0 comment

exception handling

Throw an exception with the throw statement and try The catch statement captures and processes it

Exception type

JavaScript can throw arbitrary objects. However, it is usually more efficient to use one of the following exception types to create targets

Throw statement

 throw expression;

The following code throws several different types of expressions

 throw "Error2";   //  String type throw 42;         //  Number type throw true;       //  Boolean type throw {toString: function() { return "I'm an object!"; } };

You can declare an object when an exception is thrown. Then you can query the object's attributes in the snap block. The following example creates a UserException type object myUserException to be used in the throw statement.

 // Create an object type UserException function UserException (message){ this.message=message; this.name="UserException"; } // Make the exception convert to a pretty string when used as // a string (e.g. by the error console) UserException.prototype.toString = function (){ return this.name + ': "' + this.message + '"'; } // Create an instance of the object type and throw it throw new UserException("Value too high");

try... Catch statement

The following example uses try Catch statement, in which a function is used to obtain the name of a month from an array according to the passed value. If the value does not match the month value, an exception with an "InvalidMonthNo" value will be thrown, and then set the monthName variable to unknown in the capture block statement

 function getMonthName(mo) { mo = mo - 1; //  Adjust month number for array index (1 = Jan, 12 = Dec) var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug","Sep","Oct","Nov","Dec"]; if (months[mo]) { return months[mo]; } else { throw "InvalidMonthNo"; // throw keyword is used here } } try { // statements to try monthName = getMonthName(myMonth); //  function could throw exception } catch (e) { monthName = "unknown"; logMyErrors(e); //  pass exception object to error handler -> your own function }

Snap Block

 catch (catchID) { statements }

The capture block specifies an identifier (catchID in the above statement) to store the value specified by the throw statement; You can use this identifier to get the exception information thrown

Jump to snap block when exception occurs

 try { throw "myException" // generates an exception } catch (e) { // statements to handle any exceptions logMyErrors(e) // pass exception object to error handler }

End block

Use end blocks to make your script exit gracefully when an exception occurs

 openMyFile(); try { writeMyFile(theData); // This may throw a error }catch(e){ handleError(e); //  If we got a error we handle it }finally { closeMyFile(); //  always close the resource }

If the end block returns a value, this value will be the return value of the entire try catch finally process

 function f() { try { console.log(0); throw "bogus"; } catch(e) { console.log(1); return true; //  this return statement is suspended // until finally block has completed console.log(2); //  not reachable } finally { console.log(3); return false; //  overwrites the previous "return" console.log(4); //  not reachable } // "return false" is executed now   console.log(5); //  not reachable } f(); //  console 0, 1, 3;  returns false

Error matching object

'name' provides general error classes (e.g., 'DOMException' or 'Error')

'message 'usually provides a concise message to convert the error object into a string

 function doSomethingErrorProne () { if (ourCodeMakesAMistake()) { throw (new Error('The message')); } else { doSomethingToGetAJavascriptError(); } } .... try { doSomethingErrorProne(); } catch (e) { console.log(e.name); //  logs 'Error' console.log(e.message); //  logs 'The message' or a JavaScript error message) }

Reading materials: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide

Review method: practice (write code once, run once, test once), Google where you don't understand, read and take notes

Bottom line principle: prefer rewriting once rather than copying and pasting

This review includes: exception handling

Review time: about 2 hours... I don't know why it takes so long···

Responses