Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

finally
js
throw
exception handling
try-catch

How to use try-catch statements in JavaScript

Educative Answers Team

Exception handling is the act of ensuring that code that gives an error or disrupts the running of a program is handled without giving an error.

How does try/catch help in exception handling?

The role of try/catch is as follows:

  • try: The code block that follows this statement will contain the code to try and see whether it gives an error

  • catch: The block that follows this statement will see if the code following the try statement gave an error, and decides what to do with it

With these statements, in JavaScript, you can also add a throw or a finally clause. Let’s see what role they play.

  • throw: This is a code block nested within the try statement and allows the programmer to write their own error that they want to handle

  • finally: This block contains code that will run once all the other statements have run

A piece of code may result in an error for some reason, therefore, we employ methods to handle these errors so that the program shows us what the exact error is and we may sort out what to do about the error, avoiding a program crash.


Syntax

Now that we know what each statement does and why it is used, let’s look at the syntax below:

try{
  //Code to test for error comes here
  throw //Self created error can be made here
  
}

catch(error){
  //Code to handle error comes here
}

finally{
  //Code to be executed at the end of all statements is written here
}

Examples:

Let’s look at a few simple examples on how to accomplish exception handling in JavaScript

1. Using a simple try/catch statement

In the example below, in the try part of the code, we simply call an addAlert() function which has not been defined. Given that this will give an error, we log the error to the console in the catch block.

try{
  addAlert()
}
catch(error){
  console.log(error)
  console.log("End of try-catch block")
}

2. Using try/throw/catch statements

The example below shows how we can create our own error in the throw block. Then try the code which should throw an error which should be caught by the catch block.

try{
  throw new Error ('This is a new error')
}
catch(error){
  console.log(error)
  console.log("End of try-catch block")
}

3. Using try/catch/throw/finally all together

This last example will show how to also use the finally statement with the other three statements. In this example, we do not show the entire error, rather in the catch block we just logged that the error ahs been handled.

try{
  console.log("This statement works")
  throw new Error('This statement throws an error')
}
catch(error){
  console.log("Error has been handled")
}
finally{
  console.log("Everything has been handled")
}

RELATED TAGS

finally
js
throw
exception handling
try-catch
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring