Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

As the superlator of JavaScript, TypeScript includes JavaScript content and much more, such as public/private/protected class syntax similar to Java languages. Of course, TS also implements the exception catching mechanism we’ll talk about today: throw for catching and try/catch for handling exceptions. The exception can be a default exception, a user-defined exception, or an exception type provided by a third-party dependency, such as AxiosError.

Six error types in JS

  1. SyntaxError: indicates a SyntaxError

  2. ReferenceError: Quoting something incorrectly that doesn’t exist

  3. RangeError: Out of the valid range

  4. TypeError: indicates TypeError

  5. EvalError: The eval() method uses an error

  6. URIError: The URI address is incorrect

The try/catch in TS

Try {/ /... } catch (err) {//... }Copy the code

The above example is valid code in both JS and TS.

Error type cannot be specified

For the above code, we want to specify the exact type of error in the catch.

try {
// do something 
} catch (err:TypeError) {
}

Copy the code

However, an error is reported when compiling

Catch clause variable type annotation must be ‘any’ or ‘unknown’ if specified.(1196)

The type of error must be any or unknown

The default of any | unknown cause of problems

I use err in catch (in JS, Error objects have two default properties, name and message).

The following code is perfectly fine in JS.

try {
// do something 
} catch (err) {
    console.log(err.message);
}
Copy the code

If you put it in TS, it will return an error when compiling:

In TS, the default catch error must be any or unknown. If you use err. Message directly, in TS, unknow must specify a specific type to use. If you don’t understand this, look at this example:

let u: unknown;
function user() {
 return 'Pinellia in the front'
}
u=user()
console.log(u.length)
Copy the code

As you can see in the figure below, TS defaults u to an unknown type even when assigned to string.

as

Going back to the try/catch example, if we want to use err.message, we have to specify the type of err. So we can specify the err class directly using AS.

try {
// do something 
} catch (err) {
    console.log((err as Error).message);
}
Copy the code

any

Of course the simplest is to use any directly:

try {
// do something 
} catch (err:any) {
    console.log(err.message);
}
Copy the code

By default, any | unknow reason

The default error type for a catch is any and unknow. A catch is an exception thrown by a throw. A throw can throw an exception of any type or a specific value:

Throw pinellia in the front. throw 404; / throw new Error(" throw pinxia "); / / 👍Copy the code

Since we can throw any type of exception, the simplest thing is to create an appropriate union type, but can we find such an appropriate union type? The answer is no. If you only consider basic data types and 6 wrong types, you are shallow! Do you consider that third party dependencies can also have other types of exceptions?

catch

conclusion

  1. Catch the wrong type of default must be any | unknow
  2. If you want to use error. Message directly, you can use any or as to avoid compilation errors
  3. A throw can throw any type of error