> For the complete documentation index, see [llms.txt](https://yong2401.gitbook.io/til-by-ellie/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yong2401.gitbook.io/til-by-ellie/javascript-5/error_handling.md).

# 에러 처리(Error Handling)

## 에러 처리 필요성

```js
console.log('[Start]')  // [Start]
error() // Uncaught ReferenceError: error is not defined
// 🚫프로그램 강제 종료
console.log('[End]')
```

* 프로그램이 *강제 종료되지 않고* 코드가 계속 실행되도록 에러 대응하는 방법

```js
console.log('[Start]')
try {
 error()
} catch(err) {
 console.log('Error:', err)
}
console.log('[End]')
// [Start]
// Error: ReferenceError: error is not defined
// [End]
```

## try...catch(...finally)문

* `try`: 코드블록의 에러를 테스트
* `catch`: 에러 처리
* `finally`(생략가능): 에러 발생과 상관없이 실행

  ```js
  try {
    // 실행할 코드(에러가 발생할 가능성 있는 코드)
  } catch(err) {
    // try 블록에서 에러 발생 시 실행되는 코드
    // (🔥catch에서만 유효)
    // err에 발생한 에러가 Error 객체로 전달
  } finally {
    // 에러 발생과 상관없이 한번 실행
  }
  ```

### Error 객체

```js
// 에러 객체 생성
const error = new Error(errMsg);
```

* `Error` 생산자 함수는 `Error`, `SyntaxError`, `ReferenceError`, `TypeError` 등 에러 객체를 생성하고 모두 **`Error.prototype`** 을 상속받는다.

### throw 문

```js
// 에러 발생
// throw errorExpression
try {
  throw new Error('invalid variable')
}
```

* 에러를 발생시키려면 `try`블럭 안에서 `throw`문으로 에러 던져야한다.

## 에러의 전파

* 🔥`throw`된 에러는 호출자 방향으로 전파되며 캐치하지 않으면 프로그램은 강제 종료된다.
* 🔥`setTimeout`이나 프로미스 후속 처리 메서드의 콜백함수는 호출자가 없다.

***

### Reference

🔗 [Discover JavaScript try catch | BitDegree](https://helloworldjavascript.net/pages/290-exception.html) 🔗 [예외 처리 | JavaScript로 만나는 세상](https://www.bitdegree.org/learn/javascript-try-catch)

📚[<모던 자바스크립트 Deep dive>](http://www.kyobobook.co.kr/product/detailViewKor.laf?mallGb=KOR\&ejkGb=KOR\&barcode=9791158392239)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://yong2401.gitbook.io/til-by-ellie/javascript-5/error_handling.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
