← Back to TypeScript Course | Chapter 18: Error Handling | Lesson 2 of 5

Custom Error Classes की जानकारी

Custom error classes applications को meaningful names और properties के साथ specific failure conditions represent करने देती हैं। वे built-in Error class को extend करती हैं और instanceof से narrow की जा सकती हैं।
Syntax
typescript
class CustomError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "CustomError";
  }
}

Creating a Custom Error

एक custom error class built-in Error को extend करती है और आमतौर पर this.name को एक specific string set करती है, जो logging और error-reporting tools को एक नज़र में exactly यह पहचानने देता है कि किस category की failure हुई।

उदाहरण: Creating a Custom Error

typescript
class NotFoundError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "NotFoundError";
  }
}
try {
  throw new NotFoundError("User not found");
} catch (err) {
  if (err instanceof NotFoundError) console.log(err.name, err.message);
}

Custom Error Properties

Custom errors standard message के अलावा additional typed properties भी रख सकते हैं — एक error code, एक resource id, एक validation field name — catch blocks को सिर्फ एक string parse करने के बजाय act करने के लिए structured data देते हुए।

उदाहरण: Custom Error Properties

typescript
class ValidationError extends Error {
  constructor(message: string, public field: string) {
    super(message);
    this.name = "ValidationError";
  }
}
try {
  throw new ValidationError("Required", "email");
} catch (err) {
  if (err instanceof ValidationError) console.log(err.field, err.message);
}

Multiple Custom Errors

अलग-अलग error classes अलग failure categories represent कर सकती हैं — NotFoundError, ValidationError, AuthError — और अलग-अलग catch और handle की जा सकती हैं, ताकि हर तरह की failure को एक appropriately अलग response मिले।

उदाहरण: Multiple Custom Errors

typescript
class NotFoundError extends Error {}
class ValidationError extends Error {}
class AuthError extends Error {}
function handle(err: Error) {
  if (err instanceof NotFoundError) console.log("404");
  else if (err instanceof ValidationError) console.log("400");
  else if (err instanceof AuthError) console.log("401");
}
handle(new ValidationError("bad input"));

Custom Errors in Functions

Functions custom errors throw कर सकते हैं जबकि callers instanceof checks उपयोग करके decide करते हैं कि कैसे respond करें, एक generic try/catch को specific, meaningful failure types पर एक dispatch table में बदलते हुए।

उदाहरण: Custom Errors in Functions

typescript
class NotFoundError extends Error {}
function findUser(id: number) {
  if (id !== 1) throw new NotFoundError("User not found");
  return { id, name: "Ravi" };
}
try {
  findUser(2);
} catch (err) {
  if (err instanceof NotFoundError) console.log("Handled:", err.message);
}

Best Practices for Custom Errors

Custom errors को meaningful application-level failures के लिए reserve करें जिन्हें distinguish करना worth हो, और उनकी extra properties को focused और strongly typed रखें बजाय हर error को एक loosely-typed grab bag बनाने के।

उदाहरण: Best Practices for Custom Errors

typescript
class PaymentError extends Error {
  constructor(message: string, public code: number) {
    super(message);
    this.name = "PaymentError";
  }
}
try {
  throw new PaymentError("Card declined", 402);
} catch (err) {
  if (err instanceof PaymentError) console.log(err.code, err.message);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Constructor में super(message) भूल जाना, जो this उपयोग करने से पहले required है।
  2. this.name set न करना, इसलिए error एक generic Error के रूप में print होता है।
  3. ES5 जैसे old targets में compile करते समय instanceof से catch करना, जहां prototype chain टूट जाती है जब तक आप Object.setPrototypeOf उपयोग न करें।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.