w3resource

TypeScript Error Handling - Exercises & Solutions

TypeScript Error Handling [12 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

Following exercises will help you practice error handling techniques in TypeScript, including exception handling, creating custom error classes, and propagating errors through your code.

Exception Handling:

1. Write a TypeScript function that throws an exception when a specified condition is not met. Implement a try-catch block to handle the exception and display an error message.
Click me to see the solution

2. Write a TypeScript function that calls a non-existent function. Use a try-catch block to catch the resulting error and display a custom error message.  
Click me to see the solution

3. Write a TypeScript function that parses an integer from a string. Handle potential exceptions caused by invalid input using try-catch.  
Click me to see the solution

4. Write a TypeScript program that opens a non-existent file using the Node.js 'fs' module. Implement error handling to catch file-not-found exceptions and display an error message. 
Click me to see the solution

Custom Error Classes:

5. Write a custom TypeScript error class called 'ValidationException' that extends the built-in 'Error' class. Use this custom error class to throw and catch validation errors with custom error messages.  
Click me to see the solution

6. Write a TypeScript program that reads data from an external API. Create a custom error class, 'ApiError', to handle errors related to API requests. Use this custom error class to catch and display API error messages.  
Click me to see the solution

7. Write a TypeScript function that performs database operations. Create a custom error class, 'DatabaseError', for handling database-related errors. Use this error class to catch and log database errors.  
Click me to see the solution

8. Write a TypeScript application that processes user input. Create a custom error class, `InputError`, to handle invalid input errors. Use this error class to validate user input and display appropriate error messages.  
Click me to see the solution

Error Propagation:

9. Write a TypeScript program that reads data from a file using Node.js. Implement error propagation by handling errors at different levels of your code (e.g., in a function, in middleware, and in the main application).  
Click me to see the solution

10. Write a TypeScript function that makes multiple API requests in sequence. Handle errors from each request and propagate them to the calling function. Implement logging for all errors.  
Click me to see the solution

11. Write a TypeScript program that simulates a multi-step workflow. Create custom error classes for each step of the workflow. Implement error propagation to handle and log errors at each step.  
Click me to see the solution

12. Write a TypeScript application that interacts with a database and performs transactions. Implement error propagation to handle database-related errors throughout the application, ensuring transactions are rolled back.  
Click me to see the solution

More to Come !

TypeScript Editor

See the Pen TypeScript by w3resource (@w3resource) on CodePen.


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.