Which Keywords are used to handle Exceptions in JavaScript ?

JavaScript handles exceptions through the “try..catch…finally” statement. An example statement is shown below.

filter_none

edit
close

play_arrow

link
brightness_4
code

try {
  // Attempt to execute this code
} catch (exception) {
  // This code handles exceptions
} finally {
  // This code always gets executed
}

chevron_right


The first element of a “try…catch…finally” block is the “try” clause. The “try” clause is used to limit a code block that can generate an exception and is compulsory for implementation. The “try” clause ought to be accompanied through one or each of the “catch” and “finally” clauses.

The “catch” block: The second element of “try…catch…finally” is the “catch” clause. The “catch” clause is a block of code that is most effective if an exception happens in the “try” clause. Although the “catch” clause is optional, it is not enough to deal with an exception without it. This is due to the fact that the “catch” clause stops the exception from propagating via the decision stack, permitting this system to recover. If an exception happens inside the “try” block, then it is handed over to the “catch” clause without much delay. The exception is passed off to the “catch” block for processing. The following instance shows how a “catch” clause is used to deal with a “ReferenceError”. Note that the “ReferenceError” item is included in the “catch” clause through the “exception” variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

try {
  
 // ReferenceError
  foo++;  
} catch (exception) {
  
  // It handles the exception
  var message = exception.message;  
}

chevron_right


Complex programs can generate a few exceptions. In such cases, the “instanceof” operator may be used to distinguish among the various kinds of exceptions. In the example, expect that the “try” clause can generate various kind of exceptions. The corresponding “catch” clause uses “instanceof” to handle “TypeError” and “ReferenceError” exceptions one by one from all different kinds of errors.



filter_none

edit
close

play_arrow

link
brightness_4
code

try {
  
  // Assuming an exception is occurring
} catch (exception) {
  if (exception instanceof TypeError) {
  
    // This part handles TypeError exceptions
  } else if (exception instanceof ReferenceError) {
  
    // This part handles ReferenceError exceptions
  } else {
  
    // This part handles all other 
    // types of exceptions
  }
}

chevron_right


The “finally” block: The last element of the “try…catch…finally” statement is optional, i.e. the “finally” clause. The “finally” clause is a block of code that is performed after the “try” and “catch” clauses, irrespective of any errors. The “finally” clause is beneficial for smoothing up the code (remaining files, etc). Note that the “finally” clause is even performed if an exception happens is not caught. In this type of scenario, the “finally” clause is performed after which the thrown exception proceeds normally.

The “finally” clause will be performed despite the fact that the “try” or “catch” clause executes a “return” announcement. For example, the subsequent characteristic returns fake because the “finally” clause is the last factor to execute.

filter_none

edit
close

play_arrow

link
brightness_4
code

function case() {
  try {
    return true;
  } finally {
    return false;
  }
}

chevron_right


Throwing Exceptions: JavaScript allows programmers to throw their very own exceptions using the “throw” statement. By using this, it becomes less difficult to debug and keep up for the programmer. There is no limit to the kind of statistics that may be thrown as an exception. Similarly, there is no restriction at the variety of instances that the identical statistics may be stuck and thrown.

The “throw” statement can be used with any type of data having a lot of benefits. Firefox gives debugging information for objects such as line number in the file where the exception occurred.

For example, a division operation occurs somewhere in the file. The division can cause some problems due to the division by zero resulting in “NaN” in JavaScript. This can give confusing results that are tough to debug. It can be made much simpler if the application throws exceptions about the division by zero. The following “if” statement explains the scenario by throwing an exception.

While the “throw” statement may be used with any statistics type, there are positive advantages to the use of the integrated exception types. Firefox, for instance, offers a unique remedy to the one’s gadgets via way of means of including debugging facts together with the filename and line number wherein the exception occurred.

filter_none

edit
close

play_arrow

link
brightness_4
code

if (denominator === 0)
  throw new Error("Attempted division by zero!");

chevron_right


full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.