이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
When things go wrong
- A runtime error normally crashes the program.
- An exception is Python's signal that something went wrong.
- Exception handling lets you catch it and keep running.
English
try / except
- Put risky code in a
try:block. - If it raises an exception, the
except:block runs instead of crashing. - If nothing goes wrong, the
exceptblock is skipped.

English
try:
number = int("not a number")
except ValueError:
number = 0
print(number)
English
Catch the right error
- Name the type of error you expect:
except ValueError:. ValueErroris bad text for a number;ZeroDivisionErroris divide by zero.- Catching a specific type means you do not hide other bugs.
English
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
English
finally and raise
- A
finally:block always runs, error or not — good for cleanup. raiselets you signal an error on purpose.raise ValueError("message")stops the code unless something catches it.
English
try:
raise ValueError("bad value")
except ValueError as e:
print("Caught:", e)
finally:
print("Always runs")
English
Why it matters
- A robust program should not crash on bad input.
- Handling exceptions lets it warn the user and carry on.
- It also makes sure files and other resources are closed cleanly.
English
Common mistakes
- Catch a specific error (
except ValueError:), not a bareexcept:. - Put only the risky line inside the
try.
English
Now you try
- Use
try/except(andraisein the last task). - Press Check answer to test your code.
탐색하기
try / except
Code runs in try; if it fails, control jumps to except.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Write safe_int(text) that 환원 int(text). If the text is not a valid number, catch the ValueError and return -1 instead.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Write divide(a, b) that 환원 a / b. If b is 0, catch the ZeroDivisionError and return None instead.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Write withdraw(balance, amount). If amount is more than balance, raise ValueError. Otherwise 반환 the new balance (balance - amount).
출력을 보려면 '실행'을 클릭하세요.