![]() |
VOOZH | about |
The try, catch and finally blocks are used for error handling and state management while performing asynchronous tasks. It is an effective way to handle errors and state on the web page. This tutorial will explain to you the use of the try, catch, and finally statements practically.
try{
// A block of code to check for errors
}
catch(error){
// Displays error thrown by the try block
}
finally{
// Runs irrespective of try and catch blocks
}
Example: The below code example implements the try-and-catch blocks in TypeScript.
Output:
Example: The below code example implements the try, catch and finally to fetch data from an API.
Output: