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.

Exceptions Handling in Java

In java programming, an exception is an abnormal condition that arises or occurs in a source code during run time.

In short  Exceptions are run time errors.

Example:

In the above program, Test is the name of a class.

When we try to compile this program we will receive an error message because the value of 42/d (42/0) is infinite and it can’t be possible to save and print infinite value.

Handling Java Exception

In java programwhen exception occurs it  can be managed by the java run time system or it can be manually handled by your code.

Java exception can be managed by 5 keyword  try, catch, throw, throws and finally.

  1. try : Any Program statement that we want to monitor for exception are must be placed  within a try block.
  2. catch  :  If the exception occurs within try block it is thrown. Your code can catch this exception (using catch) and handle it.
  3. throw  :  if exception arises then this system generated exception are automatically thrown by Java run time system. To manually throw an exception, throw keyword used.
  4. throws  :  A throws clause list the type of exception that a method might throw.
  5. finally  :  In programming we want any code must be execute after try block completes  is placed within a finally block.

Syntax:

Note: “try “  block can be used either with catch or finally block or with both but try cannot be used alone.

A try can have multiple catch block.

When there is exception in try block an appropriate catch block is executed based on exception.