Try Catch and Finally in JavaScript

Try Catch and Finally in JavaScript is used for exception handling.

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 final statement the code will be executed, and after try and catch, the function will return a result.

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

Error handling try and catch

In many times our scripts have errors. They are occurring because of programmer mistakes, 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 the statement allows creating a block of code for errors while it is being executed.

The catch the 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 is followed by exactly one catch block or one finally.

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

In the catch block exception in caught in the object is 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.

Here we discussed Try Catch and Finally in JavaScript. Hope you understand it.

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