This tutorial is designed to provide you with an understanding of how to handle errors in TypeScript, a statically typed superset of JavaScript. Error handling is a crucial part of any application and TypeScript provides robust tools to manage and anticipate errors in your code.
By the end of this tutorial, you will be able to:
try-catch
statements to handle runtime errors.Prerequisites: You should have a basic understanding of JavaScript and TypeScript.
A try-catch
statement in TypeScript is used to handle runtime errors. The try
block contains the code that might throw an exception, and the catch
block is used to handle the exception if one occurs.
Here's an example:
try {
// code that might throw an exception
} catch (error) {
// handle the error
}
It's a best practice to throw an instance of Error
or a subclass of Error
, as it makes it easier to handle specific error types using instanceof
in a catch
block.
In TypeScript, you can create custom error classes by extending the built-in Error
class. Custom error classes can be used to throw and catch specific errors.
Here's an example of a custom error class:
class CustomError extends Error {
constructor(message?: string) {
super(message);
this.name = "CustomError";
}
}
In TypeScript, an interface can also be used to define the shape of an error. This can be particularly useful when working with libraries or APIs that define their own error structures.
Here's an example of an error interface:
interface CustomErrorInterface extends Error {
customProperty: string;
}
Here's an example of how to use a try-catch
statement:
try {
throw new Error("An error occurred");
} catch (error) {
console.error(error.message); // "An error occurred"
}
Here's an example of how to define and use a custom error class:
class CustomError extends Error {
constructor(message?: string) {
super(message);
this.name = "CustomError";
}
}
try {
throw new CustomError("A custom error occurred");
} catch (error) {
if (error instanceof CustomError) {
console.error(error.message); // "A custom error occurred"
}
}
Here's an example of how to define an error interface and use it:
interface CustomErrorInterface extends Error {
customProperty: string;
}
let error: CustomErrorInterface = {
name: "CustomError",
message: "A custom error occurred",
customProperty: "some value",
};
console.error(error.customProperty); // "some value"
In this tutorial, you learned how to handle errors in TypeScript using the try-catch
statement. You also learned how to create custom error classes and define error interfaces.
As next steps, you could explore more about Promise rejection handling and async/await error handling in TypeScript. The TypeScript Handbook is a great resource for learning more about TypeScript.
try-catch
statement that throws and catches an Error
with your own custom message.Error
, and use it in a try-catch
statement.Solutions:
typescript
try {
throw new Error("My custom error message");
} catch (error) {
console.error(error.message); // "My custom error message"
}
try {
throw new MyCustomError("My custom error occurred");
} catch (error) {
if (error instanceof MyCustomError) {
console.error(error.message); // "My custom error occurred"
}
}
3. Solution:
typescript
interface MyCustomErrorInterface extends Error {
myCustomProperty: string;
}
let error: MyCustomErrorInterface = {
name: "MyCustomError",
message: "My custom error occurred",
myCustomProperty: "my custom value",
};
console.error(error.myCustomProperty); // "my custom value"
```
Tips for further practice: Try to use these error handling techniques in a real TypeScript project.