Try Catch and Finally in JavaScript

The try statement tests a block of code for errors.The catch statement handles the error.

The throw statement throws or creates custom errors.With finally statement the code will be executed, after try and catch, function will return a result.

Errors will happen, when executing a code, different errors can occur.

Error handling try and catch

In many time our scripts have errors. They are occurring because of programmer mistakes, an unexpected user input, or something else.

Normally, a script immediately stops in case of an error, printing it as an error massage.

But there is a syntax construct try, catch that allows to catching errors and, instead of stop the program, do something with programmer custom massage.

The try statement consists of a try block, which contains one or more statements. {} always be used in try block.

One catch clause, or a finally clause, must be after try block to handle the error.

This gives three blocks for the try statement:

try, catch

try, finally

try, catch, finally

try and catch

The try statement allows creating a block of code for errors while it is being executed.

The catch statement allows creating a block of code to be executed, if there is any error occurs in the try block.

Syntax

OUTPUT

OUTPUT

The try, catch, finally Statement

Try catch finally are so called exception handling statements.

An exception is an error that occurs at run time due to an error operation during execution.

Examples of exceptions include trying to reference an undefined variable, or calling a nonexistent method.

Examples of syntax errors of exceptions:

•        alert(“I am missing a closing part //syntax error

•        alert(xy) //exception occur “xy” isn’t defined yet

•        undefined function() //exception

Normally whenever runs an exception somewhere in code, it displays an error message to the user while aborting the execution of the remaining code.

At its simplest to use try/catch to try and run some code, and in the event of any exceptions,

User can catch programmer-generated and runtime exceptions, but cannot catch Js syntax errors.

The try block followed by exactly one catch block or one finally.

When an exception occurs in the statement written inside try block, the control transferred to catch block.

In catch block exception in caught in object declared in catch “err” and the catch block is executed.

The optional finally block executes after try/catch

If exception is thrown from try block then only catch block is executed.

Nested try/catch/finally statements

Try should never be defined just by itself, but always followed by catch or finally.

Within each clause, you can define additional try/catch/finally statements following the same rule.

Caught the exception in the inner try-block by adding a catch block

Example

OUTPUT

Re-throw the error

The throw Statement

throw statement to raise your built-in exceptions or customized exceptions. These exceptions can be captured and you can take an action.

The throw statement allows to create a custom error.

The exception can be a JS String, a Number, a Boolean or an Object:

throw “Too big”;    // throw a text

throw 500;          // throw a number

If programmer use throws together with try and catch, you can control program flow and generate custom error messages.

The finally Statement

The finally statement lets you execute code, after try and catch.

Read More

  1. Get multiple checkbox value in javascript
  2. Add active class onclick
  3. JavaScript Multiplication Table
  4. isPrime() Javascript : Prime number program
  5. Form Handling and Validation
  6. Math Object in JavaScript