- try
- except
Open file exception handling
Exception handling
examples/files/open_file_handle.py
filename = 'examples/files/unicorns.txt' try: with open(filename, 'r') as fh: lines = fh.read() except Exception as err: print('There was some error in the file operations.') print(err) print(type(err).__name__) print('Still running.')
There was some error in the file operations. [Errno 2] No such file or directory: 'examples/files/unicorns.txt' FileNotFoundError Still running.