codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

TypeScript

42 / 60 topics
42Error Handling in TypeScript43Try/Catch44Custom Errors
Tutorials/TypeScript/Error Handling in TypeScript
🔷TypeScript

Error Handling in TypeScript

Updated 2026-05-15
10 min read

Error Handling in TypeScript

Introduction

In any programming language, error handling is a critical aspect of building robust and reliable software. TypeScript, being a superset of JavaScript, provides several mechanisms to handle errors effectively. Understanding these strategies will help you write more resilient applications that can gracefully manage unexpected situations.

Error handling in TypeScript primarily revolves around the use of exceptions, which are thrown and caught using try...catch blocks. Additionally, TypeScript offers type safety features that can prevent certain types of errors from occurring at compile time. This tutorial will cover various strategies for handling errors in TypeScript applications.

Concept

Exceptions

Exceptions are runtime errors that disrupt the normal flow of a program. In JavaScript (and therefore TypeScript), exceptions are thrown using the throw keyword and caught using try...catch blocks.

Throwing an Exception

You can throw an exception using the throw keyword followed by an expression. The expression can be any value, but it is common to use instances of built-in error types like Error, TypeError, or RangeError.

TypeScript
1function divide(a: number, b: number): number {
2if (b === 0) {
3 throw new Error("Division by zero is not allowed.");
4}
5return a / b;
6}

Catching an Exception

To catch an exception, you use the try...catch block. The code that might throw an exception is placed inside the try block, and the code to handle the exception is placed inside the catch block.

TypeScript
1try {
2const result = divide(10, 0);
3console.log(result);
4} catch (error) {
5console.error(error.message); // Output: Division by zero is not allowed.
6}

Type Safety

TypeScript's type system can help prevent many errors before they occur at runtime. By defining types for variables and function parameters, you can ensure that the correct data types are used throughout your application.

Example of Type Safety

Consider a function that expects an array of numbers and returns their sum:

TypeScript
1function sum(numbers: number[]): number {
2return numbers.reduce((acc, num) => acc + num, 0);
3}
4
5const total = sum([1, 2, 3, 4]); // Correct usage
6console.log(total); // Output: 10
7
8// This will cause a compile-time error because 'string' is not assignable to type 'number[]'
9// const invalidTotal = sum(["one", "two", "three"]);

Optional Chaining and Nullish Coalescing

Optional chaining (?.) and nullish coalescing (??) are features introduced in TypeScript that help handle potential null or undefined values gracefully.

Optional Chaining

Optional chaining allows you to safely access deeply nested properties without having to check each level for null or undefined.

TypeScript
1interface User {
2name: string;
3profile?: {
4 age: number;
5};
6}
7
8const user: User = { name: "John" };
9
10// Without optional chaining
11if (user.profile && user.profile.age) {
12console.log(user.profile.age);
13} else {
14console.log("Age is not available.");
15}
16
17// With optional chaining
18console.log(user.profile?.age ?? "Age is not available."); // Output: Age is not available.

Nullish Coalescing

Nullish coalescing allows you to provide a default value when dealing with null or undefined.

TypeScript
1const age = user.profile?.age ?? 18;
2console.log(age); // Output: 18

Examples

Example 1: Basic Try/Catch Block

Let's create a simple function that reads a file and handles potential errors.

TypeScript
1function readFile(filePath: string): string {
2if (!filePath) {
3 throw new Error("File path is required.");
4}
5// Simulate reading a file
6return "File content";
7}
8
9try {
10const content = readFile("");
11} catch (error) {
12console.error(error.message); // Output: File path is required.
13}

Example 2: Using Type Guards

Type guards are functions that check the type of a variable at runtime. They can be used to handle different types of errors.

TypeScript
1function assertIsNumber(value: any): asserts value is number {
2if (typeof value !== "number") {
3 throw new TypeError("Value must be a number.");
4}
5}
6
7try {
8assertIsNumber("not a number");
9} catch (error) {
10console.error(error.message); // Output: Value must be a number.
11}

Example 3: Handling Promises

When dealing with asynchronous operations, you can use try...catch blocks within async functions to handle errors.

TypeScript
1async function fetchData(url: string): Promise<string> {
2if (!url) {
3 throw new Error("URL is required.");
4}
5// Simulate fetching data
6return "Data from URL";
7}
8
9(async () => {
10try {
11 const data = await fetchData("");
12} catch (error) {
13 console.error(error.message); // Output: URL is required.
14}
15})();

What's Next?

In the next section, we will explore more advanced error handling techniques, including using finally blocks and custom error classes. We'll also delve into how to handle errors in asynchronous code using async/await.

Stay tuned for more insights into TypeScript's powerful features!


PreviousAsync/AwaitNext Try/Catch

Recommended Gear

Async/AwaitTry/Catch