Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Python
|
Handling exceptions
Handling exceptions
Handling exceptions
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)
© 2010-2022
mti
tek