How to use Try Catch and Finally in TypeScript ? Last Updated : 29 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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.Explanation:try: The try block will run a block of code and check for its correctness and errors.catch: The catch block will catch the error thrown by the try block and print it to your desired location.finally: The final statement runs always irrespective of try and catch, it is independent of them.Syntax: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. JavaScript function codeForTryBlock(a: number, b: number) { const num1: number = a; const num2: number = b; if (num1 === num2) { console.log("Provided Numbers are same!!"); } else { throw new Error("Provided numbers are not same!!"); } } function demoImplementation() { try { codeForTryBlock(3, 5); } catch (error: any) { console.error("An error Occured: ", error.message); } finally { console.log( "This text is from finally block it runs irrespective of try-catch blocks."); } } demoImplementation(); Output:Example: The below code example implements the try, catch and finally to fetch data from an API. JavaScript async function fetchGET(url: string): Promise<any> { try { const response = await fetch(url); if (!response.ok) { throw new Error( `Unable to Fetch Data, Please check URL or Network connectivity!!` ); } const data = await response.json(); return data.results[0]; } catch (error: any) { console.error("An Error Occured: ", error.message) } finally { console.log( "This code is from finally block of fetchGET function and it will run always."); } } async function fetchData() { const returnedData = await fetchGET('https://randomuser.me/api'); console.log(returnedData); } fetchData(); Output: Comment More infoAdvertise with us Next Article How to use Try Catch and Finally in TypeScript ? A abhish8rzd Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads How to Type an Async Function in TypeScript ? An asynchronous function allows your code to do multiple things at once. It doesn't block the execution of the rest of your code while waiting for a long task (like reading a file, making an API request) to finish. Instead, it returns a Promise, making it easier to handle operations that might take 3 min read How to Use Fetch in TypeScript ? In TypeScript, fetching the data from any server or API using fetch() API is not the same as we used to do it in Plain or Vanilla JavaScript. Let us see how we can use the fetch API to fetch data from any server or API in TypeScript.NOTE: Before using this code, please make sure that you have jQuery 4 min read How To Use @ts-ignore For A Block In TypeScript? The @ts-ignore is a compiler that lets the compiler ignore the line below it which is like a directive used to suppress TypeScript compiler errors upcoming on the next line of the whole code. This is mostly used when we as the developers know the code is good and useful but the compiler flags it as 3 min read How to solve too many try catch in Typescript ? In this article, we will try to see how we actually write too many try/catch blocks for catching multiple errors in TypeScript, and further, we will try to understand by which technique we may be able to reduce the overhead of writing too many try/catch blocks, with the help of certain coding exampl 4 min read How to use Async/Await with a Promise in TypeScript ? In TypeScript, you can use async and await with a Promise to simplify asynchronous code and make it more readable.What is a Promise?A promise in TypeScript is an object representing the eventual completion or failure of an asynchronous operation. It acts as a placeholder for a value that may be avai 3 min read Like