---
type: snippet
tags: ['error']
status: release
ctime: 2023-07-03
mtime: 2024-03-22
---

```ts
class CustomError extends Error {
  name = 'CustomError'

  constructor(message?: string) {
    super(message)

    Object.setPrototypeOf(this, CustomError.prototype)
  }
}

try {
  throw new CustomError('This is a custom error message.')
} catch (error) {
  if (error instanceof CustomError) {
    console.log('CustomError occurred:', error.message)
    console.log('Error name:', error.name)
  } else {
    console.log('An error occurred:', error)
  }
}
```

- https://github.com/Alex-D/check-disk-space/blob/main/src/errors/invalidPathError.ts
- https://github.com/adriengibrat/ts-custom-error/blob/main/src/custom-error.ts
- [📘 타입스크립트 커스텀 Error 처리하기](https://inpa.tistory.com/entry/TS-%F0%9F%93%98-%ED%83%80%EC%9E%85%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EC%BB%A4%EC%8A%A4%ED%85%80-Error-%EC%B2%98%EB%A6%AC%ED%95%98%EA%B8%B0)
- [Handling errors like a pro in TypeScript | by Kolby Sisk | Udacity Eng & Data](https://engineering.udacity.com/handling-errors-like-a-pro-in-typescript-d7a314ad4991)