In JavaScript, try/catch
statements are use to handle exceptions present in the try
code blocks.
After checking and encountering an error in the statements in the try
block, the exception block is called. If the exception encountered is the same as the exception handled in the catch
block, then the control immediately shifts to the catch
block, and the statements within the catch
block are executed.
The statements presented after the erroneous statement will not be executed, unless there is a return
statement in the finally
block.
A good practice is to use a conditional catch clause first if we anticipate that any exception will occur. An unconditional catch clause is placed last to handle all the remaining exceptions:
try { demotrycatchfunction(); // may throw any type of exceptions } catch (e) { if (e instanceofReferenceError) { // statements to handle ReferenceErrorexceptions } else if (e instanceofSyntaxError ) { // statements to handle SyntaxError exceptions } else if (e instanceofEvalError) { // statements to handle EvalError exceptions } else { // statements to handle any unspecified exceptions logMyError(e); // pass the exception object to the error handler } }
This block will execute statements present within at end, whether an exception has occurred or not. The statements in a finally
block execute irrespective of whether an error occurred. The finally
block generally contains code that must be executed regardless of anything. Hence, we generally release resources and close connection inside a finally
block. The syntax for writing a simple try-catch-finally
block is as follows:
try { //try code - This is the Code block to try } catch(error) { //catch code – This is the Code block that handle errors } finally { //finally code - This is the Code block to be executed regardless of try catch results }
We can also write this with a nested catch
blocks like this:
try { //do something } catch (Exception e) { try { //do something with little likeliness of output } catch (Exception ex) { try { //do the minimum acceptable } catch (Exception e1) { //More try catches? } } }
Here is an example showing the working of multiple catch
clauses:
functionCheckEligibility(Age) { var result; try { //try block if (Age < 16 { //condition to be tested throw new Error("Children below the age of 16 are not allowed. Parent Supervision needed!"); //incase of false result, an error will be raised } result = age; } catch (e) { //catch block console.log(e.toString()); //error is converted to string and logged into the console throw e; //Uncaught Error } finally { // finally block - It will run in the end regardless of the try, catch results console.log("Age doesn't matter!"); } return result; };
Let's pass 14 as the argument to the function, as shown here:
CheckEligibility(14);
The output will be as follows:
Children below the age of 16 are not allowed. Parent Supervision needed!
In try-catch
statements, we must have at least one finally
or try-catch
block. Try
doesn't necessarily need a catch
clause. If a try
statement does not contain at least one catch
block, it must contain a finally
block. The possible exception handling clauses with try-catch-finally
are the try-catch
, try-finally
, or try-catch-finally
clauses.
3.138.37.20