Skip to content


.js – Statements

JavaScript: Handling Exceptions

the try... catch statement – exception
handler

The try...catch statement marks a block of
statements to try, and specifies one or more responses should an
exception be thrown. If an exception is thrown, the
try...catch statement catches it.

The try...catch statement consists of a
try block, which contains one or more statements, and
zero or more catch blocks, containing statements that
specify what to do if an exception is thrown in the try block. That
is, you want the try block to succeed, and if it does
not succeed, you want control to pass to the catch
block. If any statement within the try block (or in a
function called from within the try block) throws an
exception, control immediately shifts to the catch
block. If no exception is thrown in the try block
succeed, the catch block is skipped. The
finally block executes after the try and
catch blocks execute but before the statements
following the try...catch statement.

The following example uses a try...catch statement.
The example calls a function that retrieves a month name from an
array based on the value passed to the function. If the value does
not correspond to a month number (1-12), an exception is thrown
with the value InvalidMonthNo and the statements in
the catch block set the monthName
variable to unknown.

function getMonthName (mo) {
    mo=mo-1; // Adjust month number for array index (1=Jan, 12=Dec)
    var months=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul",
          "Aug","Sep","Oct","Nov","Dec");
    if (months[mo] != null) {
       return months[mo]
    } else {
       throw "InvalidMonthNo"
    }
}

try {
// statements to try
    monthName=getMonthName(myMonth) // function could throw exception
}
catch (e) {
    monthName="unknown"
    logMyErrors(e) // pass exception object to error handler
}

The catch Block

You can use a single catch block to handle all
exceptions that may be generated in the try block, or
you can use separate catch blocks each of which
handles a particular type of exception.

Single catch Block
Use a single try...catch statement’s
catch block (recovery block) to execute error-handling
code for any exceptions thrown in the try block.

A single catch block has the following syntax:

catch (catchID) {
  statements
}

The catch block specifies an identifier
(catchID in the preceding syntax) that holds the value
specified by the throw statement; you can use this
identifier to get information about the exception that was thrown.
JavaScript creates this identifier when the catch
block is entered; the identifier lasts only for the duration of the
catch block; after the catch block
finishes executing, the identifier is no longer available.

For example, the following code throws an exception. When the
exception occurs, control transfers to the catch
block.

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

Core JavaScript Guide: Exception
Handling Statements: try… catch statement;


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

You must be logged in to post a comment.



Read more:
XSS Preparedness & a Friendly Programmer’s Advice

PHP Input Validation Flaw in Certain Error Pages as reported on Security Tracker dotCom, and other such news always raises my ears because I...

Close