TGViewer
JavaScript JavaScript @javascript · 31.1K subscribers
Post #3057 2.67K
CHALLENGE

class AppError extends Error {
constructor(message, code) {
super(message);
this.name = "AppError";
this.code = code;
}
}

function riskyOperation(value) {
if (value === null) throw new AppError("Null value", 404);
if (typeof value !== "number") throw new TypeError("Not a number");
if (value < 0) throw new RangeError("Negative value");
return value * 2;
}

const inputs = [42, null, "hello", -5];
const results = inputs.map((input) => {
try {
return riskyOperation(input);
} catch (err) {
if (err instanceof AppError) return `AppError:${err.code}`;
if (err instanceof TypeError) return `TypeError`;
if (err instanceof RangeError) return `RangeError`;
return `UnknownError`;
}
});

console.log(results);
  • ❤ 5
  • 👍 2
  • 🔥 2
More from @javascript
  1. Sep 24, 2026Post #3516
  2. Sep 24, 2026CHALLENGE class Base { static count = 0; static increment() { this.count++; return this.co…
  3. Sep 23, 2026🤩 tinyjs: Build JavaScript Desktop Apps in <10MB I built an app with this and was impress…
  4. Sep 23, 2026Post #3513
  5. Sep 23, 2026CHALLENGE function* inner() { yield 1; yield 2; return 100; } function* outer() { const re…
  6. Sep 22, 2026Post #3511
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →