> For the complete documentation index, see [llms.txt](https://nmdkims-organization.gitbook.io/python-hand-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nmdkims-organization.gitbook.io/python-hand-book/day3./1.-try-except.md).

# 1. 예외 처리 (try, except)

예외처리 아주 편리한 방법입니다. 하지만 너무 무분별하게 사용하면 안되요

## 예외 처리

코딩을 하다보면 생각하지 못한 여러가지 오류가 발생할 것입니다.

이런 상황에서 에러를 핸들링 하기 위해 파이썬에서 "try", "except문"을 사용 할 수 있습니다.

### try, except 문

```python
try:
    ...
except :
    ...
```

위 경우 에러 종류에 상관없이 에러가 발생하면 except 블록을 수행합니다.

```python
try:
    ...
except 발생오류:
    ...
```

이 경우에는 정해 놓은 오류와 동일한 오류가 발생 했을 때만 except 블록을 수행하게 됩니다.

```python
try:
    4 / 0
except ZeroDivisionError as e:
    print(e)

# division by zero
```

위 경우는 오류의 내용까지 알고 싶을때 쓰는 방법 입니다.

에러가 발생하면 "e" 라는 변수에 해당 에러의 종류가 저장되고 이것을 "print()"를 이용해서 출력할 수 있게 만들어 주는 것 입니다.
