How to solve too many try catch in Typescript?


We can use the try-catch statements to solve errors in TypeScript. Sometimes, we require adding more than one try-catch block in the code to handle multiple errors.

When we add multiple try-catch statements in the code, the code becomes unreadable, and it becomes a headache for developers to refactor. In this tutorial, we will learn to convert too many try-catch blocks into a single try-catch block which can manage multiple errors.

Syntax

Users can follow the syntax below to use the single try-catch block in TypeScript.

try {
   throw new Error("error_message");
   // this code will not be executed
}
catch (error) {
   // manage the error
}

In the above syntax, we throw the errors inside the try block and catch the error in the catch block.

Whenever we get any error in the try block, execution control directly goes to the catch statement without executing the other try block code.

Example 1: Too many try-catch blocks

In the example below, we have added four try-catch blocks. We are throwing errors with different messages from every try-catch block.

Users can see the error message printed from every catch block in the output.

try {
   console.log("Inside the first try block");
   throw new Error("first error message");
} catch (error) {
   console.log(error.message);
}

try {
   console.log("Inside the second try block");
   throw new Error("second error message");
} catch (error) {
   console.log(error.message);
}

try {
   console.log("Inside the third try block");
   throw new Error("Third error message");
} catch (error) {
   console.log(error.message);
}

try {
   console.log("Inside the fourth try block");
   throw new Error("Fourth error message");
} catch (error) {
   console.log(error.message);
}

On compiling, it will generate the following JavaScript code.

try {
   console.log("Inside the first try block");
   throw new Error("first error message");
}
catch (error) {
   console.log(error.message);
}
try {
   console.log("Inside the second try block");
   throw new Error("second error message");
}
catch (error) {
   console.log(error.message);
}
try {
   console.log("Inside the third try block");
   throw new Error("Third error message");
}
catch (error) {
   console.log(error.message);
}
try {
   console.log("Inside the fourth try block");
   throw new Error("Fourth error message");
}
catch (error) {
   console.log(error.message);
}

Output 

The above code will produce the following output –

Inside the first try block
first error message
Inside the second try block
second error message
Inside the third try block
Third error message
Inside the fourth try block
Fourth error message

From the above example, users can understand how code becomes unreadable and unclear when we use too many try-catch statements in a single code.

Now, we will learn to use the single try-catch block to handle multiple code blocks with different errors.

Example 2

In the example below, we have created the solveProblems() function. It takes a function as a parameter and invokes that function in the try block. If the function throws any error, we can catch it in a single block.

function func2() {
   throw new Error("This error is from second function!");
}

function func3() {
   let num = 10;
   num.toPrecision(1000);
}
function solveProblems(func: any) {
   // calling the callback function in the try block
   try {
      func();
   } catch (error) {
      console.log(error.message);
   }
}

// calling functions
solveProblems(func2);
solveProblems(func3);

On compiling, it will generate the following JavaScript code −

function func2() {
   throw new Error("This error is from second function!");
}
function func3() {
   var num = 10;
   num.toPrecision(1000);
}
function solveProblems(func) {
   // calling the callback function in the try block
   try {
      func();
   }
   catch (error) {
      console.log(error.message);
   }
}
// calling functions
solveProblems(func2);
solveProblems(func3);

Output 

The above code will produce the following output –

This error is from second function!
toPrecision() argument must be between 1 and 100

From the above example, users can understand how we can remove the multiple try-catch block by replacing it with a single try-catch block. Users just need to create a separate function for every separate block of code and can invoke every function one by one in the single try block.

Updated on: 21-Feb-2023

805 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements