Home
LLMs
Docker
Kubernetes
Java
Python
Ubuntu
Maven
Archived
About
Python
|
Exception Handling
Exception handling lets your program gracefully manage errors and unexpected situations, instead of crashing abruptly.
Catching ZeroDivisionError:
try: i = 1 / 0 except ZeroDivisionError: print('ZeroDivisionError') else: print(i)
Catching FileNotFoundError:
from pathlib import Path path = Path('file.txt') try: text = path.read_text(encoding='utf-8') except FileNotFoundError: print('FileNotFoundError') else: print(text)
Failing silently:
try: i = 1 / 0 except ZeroDivisionError: pass # special statement that tells python to swallow the exception else: print(i)
© 2025
mti
tek