Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do try catch and finally statements in TypeScript?

I have error in my project, and I need to handle this by using try, catch and finally.

I can use this in JavaScript but not in Typescript.

When I put Exception as argument in typescript catch statement, why it is not accepting this?

here is the code.

private handling(argument: string): string {     try {         result= this.markLibrary(argument);     }     catch(e:Exception){         result = e.Message;     }     return result; } 

I need an exception message here but I can't get. And I got the below error.

Catch clause variable cannot have a type annotation.

like image 512
Raja Avatar asked Feb 12 '19 11:02

Raja


People also ask

How do you use try and catch in TypeScript?

The try catch in TypeScript statement provides a way to handle some or all of the errors that may occur in an application. These errors are often referred to as an exception. In a try-catch statement, you code a try block that contains the statements that may throw an exception.

Is there try catch in TypeScript?

Unfortunately Javascript does not support multiple catch(error) to allow you to run a different code based on the error type. But, there are ways we can improve error handling in our single catch statement to leverage the power of classes and the instanceof operator.

How do you use try catch and finally?

The try statement defines the code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result. The throw statement defines a custom error.

Can we use try catch and finally together?

Answer: No. You cannot throw the exception and also catch it in the same method. The exception that is declared using throws is to be handled in the calling method that calls the method that has thrown the exception.


2 Answers

Edit

Typescript 4.0 added the ability to specify unknown and any on catch variables (Issue) And typescript 4.4 added the ability to make unknown the default on catch variables (PR) uning the useUnknownInCatchVariables flag.

With this flag the following is now possible:

catch(e){     result = e.message; // error under useUnknownInCatchVariables      if (typeof e === "string") {         e.toUpperCase() // works, `e` narrowed to string     } else if (e instanceof Error) {         e.message // works, `e` narrowed to Error     } } 

Specifying arbitrary types on catch variables is still not supported.

Original answer

Typescript does not support annotations on the catch variable. There is a proposal to allow this but it is still being discussed (see here)

Your only solution is to use a type assertion or an extra variable

catch(_e){     let e:Error= _e;     result = e.message; }  catch(e){     result = (e as Error).message; } 

Unfortunately this will work as well and is completely unchecked:

catch(e){     result = e.MessageUps; } 

Note

As you can read in the discussion on the proposal, in JS not everything that is thrown has to be an Error instance, so beware of this assumption

Maybe tslint with no-unsafe-any would help catch this.

like image 142
Titian Cernicova-Dragomir Avatar answered Oct 08 '22 03:10

Titian Cernicova-Dragomir


With TypeScript 4.0, you can set unknown as catch clause variable type:

unknown is safer than any because it reminds us that we need to perform some sorts of type-checks before operating on our values. (docs)

try {  /* ... */ } catch (e: unknown) { // <-- note `e` has explicit `unknown` type     e.message // errors     if (typeof e === "string") {         e.toUpperCase() // works, `e` narrowed to string     } else if (e instanceof Error) {         e.message // works, `e` narrowed to Error     }     // ... handle other error types  } 

Playground

Update: TypeScript 4.4 provides a config flag --useUnknownInCatchVariables to let catch-variables default to type unknown. This is also automatically enabled with the --strict flag.

like image 43
ford04 Avatar answered Oct 08 '22 03:10

ford04