Q34. Why should catch errors be unknown?

JavaScript permits throwing arbitrary values, so a catch variable cannot safely be assumed to be an Error. Narrow with instanceof Error or validate the expected shape before reading properties. Handle non-Error thrown values deliberately.

An Error instance has a message property, but a thrown string or arbitrary object may not follow that shape.

Note

Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.

TypeScript concept
// Illustrative TypeScript contract
type Identifier = string;
const id: Identifier = "item-1";
💡 The example demonstrates the compile-time contract discussed in this question.
Key Takeaways
  • Explain the concept in your own words.
  • Know the practical trade-off and its limits.
  • Use a small example to demonstrate the idea.
Knowledge Check

What is a safe first check before reading `error.message`?