How to handle JavaScript Errors with
                         Try, Catch, Finally and Throw

How to handle JavaScript Errors with Try, Catch, Finally and Throw

In this tutorial, you will learn about the try...catch...finally and throw statements to handle exceptions in JavaScript .

Errors due to wrong input or other unforeseen things, and errors always will be made by programmers, and it’s good to know how to properly handle them.

The try, catch and finally blocks are used to handle exceptions (a type of an error). Before you learn about them, you need to know about the types of errors in programming.

Types of Errors

In programming, there can be two types of errors in the code:

  • Syntax Error: Error in the syntax. For example, if you write consol.log('your result');, the above program throws a syntax error. The spelling of console is a mistake in the above code.

  • Runtime Error: This type of error occurs during the execution of the program. For example, calling an invalid function or a variable.

These errors that occur during runtime are called exceptions. Now, let's see how you can handle these exceptions.

The try statement lets you test a block of code for errors

The catch statement lets you handle the error

The throw statement lets you create custom errors

The finally statement lets you execute code after try and catch regardless of the result

try...catch statement

The try...catch statement is used to handle the exceptions. Its syntax is:

try {
    // body of try
} 
catch(error) {
    // body of catch  
}

The main code is inside the try block. While executing the try block, if any error occurs, it goes to the catch block. The catch block handles the errors as per the catch statements.

If no error occurs, the code inside the try block is executed and the catch block is skipped.

Finally statement

How does Finally works in JavaScript?

  • The finally block is always used with a try-catch statement.
  • There can be only try statement or try with catch statement along with finally block.
  • The try block defines the code to be executed which may cause the error.
  • This error may cause due to some runtime issue or a programmer error.
  • When this code in try block will fail the catch block will be executed.
  • In the catch block, the necessary action can be taken in case of failure.
  • In case of execution of either try block or catch block, the finally block will be executed in any condition.
  • The usage of catch and finally statements is not compulsory to be used both at a time but at least one of them should be used while using a try statement.

This is the syntax of finally() block along with try and catch blocks in JavaScript −

try{
   //The code where the chances of error can be predicted 
}
catch{
   //The code which will be executed if try block is executed
}
finally{
   //The code which will be definitely executed after the try and catch blocks.
}

try.png

throw Statement

The throw statement can be used within a try block of code to create your own run-time errors.

When an exception occurs in the try block, JavaScript will normally stop, and control will be passed to the catch block with a user-defined exception (message).

Note: If no catch block exists among caller functions, the program will terminate.

Syntax

throw "custom error text"

Key Takeaways

  • Error handling handles the runtime errors in the JavaScript
  • try is the keyword creates the try block. Here the code that can cause exception is under the wrap under this block.
  • Thecatchis the keyword that creates the catch block. Additionally, we use it to catch the exception thrown from the try block and execute the code.
  • Finally, the block runs a piece of code always whether there is an exception or not.
  • The throw keyword can create customized exceptions, which can be thrown based on certain conditions.

Did you find this article valuable?

Support Chhakuli Zingare by becoming a sponsor. Any amount is appreciated!