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.
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.
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.
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.
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.
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.
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.
With TypeScript 4.0, you can set unknown
as catch clause variable type:
unknown
is safer thanany
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With