KoderSolution Logo
HomeArticlesTutorialsForumAI LabRun Code
KoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Engineering Newsletter

Join 100,000+ engineers receiving curated high-signal content weekly.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
🔷

TypeScript

Topic Hub & Articles

TypeScript Intro

10 min

TypeScript Getting Started

10 min

TypeScript Simple Types

10 min

TypeScript Explicit & Inference

10 min

TypeScript Special Types

10 min

TypeScript Arrays

10 min

TypeScript Tuples

10 min

TypeScript Object Types

10 min

TypeScript Enums

10 min

TypeScript Aliases & Interfaces

10 min

TypeScript Union Types

10 min

TypeScript Functions

10 min

TypeScript Casting

10 min

TypeScript Classes

10 min

TypeScript Basic Generics

10 min

TypeScript Utility Types

10 min

TypeScript Keyof

10 min

TypeScript Null & Undefined

10 min

TypeScript Definitely Typed

10 min

TypeScript 5 Updates

10 min

TypeScript Configuration

10 min

TypeScript Tooling

10 min

TypeScript Advanced Types

10 min

TypeScript Type Guards

10 min

TypeScript Conditional Types

10 min

TypeScript Mapped Types

10 min

TypeScript Type Inference

10 min

TypeScript Literal Types

10 min

TypeScript Namespaces

10 min

TypeScript Index Signatures

10 min

TypeScript Declaration Merging

10 min

TypeScript with Node.js

10 min

TypeScript with React

10 min

TypeScript Async Programming

10 min

TypeScript Decorators

10 min

TypeScript in JS Projects

10 min

TypeScript Migration

10 min

TypeScript Error Handling

10 min

TypeScript Best Practices

10 min

Progress
0%

0 / 39 Lessons

TypeScriptTypeScript Ecosystem & Integrations
Lesson

TypeScript Error Handling

10 min reading
Free Course

TypeScript Error Handling: Unknown Errors & Custom Error Classes

In strict TypeScript, errors caught inside try...catch blocks are typed as unknown (or any). Proper error handling requires narrowing caught errors before accessing properties like error.message.

Error Handling Architecture Flow

flowchart TD
    A["try { ... } catch (err)"] --> B["err is typed as unknown"]
    B --> C{"err instanceof Error?"}
    C -- "Yes" --> D["Access err.message & err.stack cleanly"]
    C -- "No" --> E["Handle raw string / unknown throw payload"]

Practical Code Example

// Custom Domain Error Class
export class DatabaseConnectionError extends Error {
  public readonly code: string = "DB_CONN_FAILED";

  constructor(message: string, public readonly host: string) {
    super(message);
    this.name = "DatabaseConnectionError";
    Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
  }
}

function connectToDatabase(host: string): void {
  if (host === "invalid_host") {
    throw new DatabaseConnectionError("Failed to resolve database host", host);
  }
  console.log(`Successfully connected to ${host}`);
}

// Type-Safe Error Handling
try {
  connectToDatabase("invalid_host");
} catch (err: unknown) {
  if (err instanceof DatabaseConnectionError) {
    console.error(`[${err.code}] ${err.message} (Host: ${err.host})`);
  } else if (err instanceof Error) {
    console.error(`Standard Error: ${err.message}`);
  } else {
    console.error("Unknown throw payload:", err);
  }
}

Best Practices & Gotchas

  • Always Reset Prototype Chain in Custom Errors: Call Object.setPrototypeOf(this, CustomError.prototype) inside custom Error constructors to ensure instanceof works properly across ES targets.
  • Never Throw Raw Strings: Always throw new Error(...) instead of throwing raw primitives like throw "something failed".
  • Use Result Types for Expected Operations: Consider returning { ok: true, value: T } | { ok: false, error: E } instead of relying on throw for non-exceptional business logic.

Self-Check Challenge

What is the type of err inside catch (err) when useUnknownInCatchVariables or strict is enabled? (Answer: unknown)

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Recommended Courses & Books

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum