---
description: try, except, else, finally
---

# Try except for exception handling

## The try except block <a href="#the-try-except-block" id="the-try-except-block"></a>

```python
try:
    file = open('exists.txt')
    print('File exists')
except Exception:
    print('File does not exist')
```

```
File exists
```

```python
try:
    file = open('missing.txt')
    print('File exists')
except Exception:
    print('File does not exist')
```

```
File does not exist
```

## Specifying the exception type <a href="#specifying-the-exception-type" id="specifying-the-exception-type"></a>

```python
try:
    file = open('exists.txt')
    print('File exists')
except FileNotFoundError:
    print('File does not exist')
except Exception:
    print('An error occurred')
```

```
File exists
```

```python
try:
    file = open('missing.txt')
    print('File exists')
except FileNotFoundError:
    print('File does not exist')
except Exception:
    print('An error occurred')
```

```
File does not exist
```

## Catching specific exception information

```python
try:
    file = open('exists.txt')
    print('File exists')
except FileNotFoundError as e:
    print('File does not exist:', e)
except Exception as e:
    print('An error occurred:', e)
```

```
File exists
```

```python
try:
    file = open('missing.txt')
    print('File exists')
except FileNotFoundError as e:
    print('File does not exist:', e)
except Exception as e:
    print('An error occurred:', e)
```

```
File does not exist: [Errno 2] No such file or directory: 'missing.txt'
```

## Using else in a try except block <a href="#using-else-in-a-try-except-block" id="using-else-in-a-try-except-block"></a>

```python
try:
    file = open('exists.txt')
    print('File exists')
except FileNotFoundError as e:
    print('File does not exist:', e)
except Exception as e:
    print('An error occurred:', e)
else:
    print(file.read())
    file.close()
```

```
File exists
Here are the file contents.
```

```python
try:
    file = open('mising.txt')
    print('File exists')
except FileNotFoundError as e:
    print('File does not exist:', e)
except Exception as e:
    print('An error occurred:', e)
else:
    print(file.read())
    file.close()
```

```
File does not exist: [Errno 2] No such file or directory: 'mising.txt'
```

## Using finally in a try except block <a href="#using-finally-in-a-try-except-block" id="using-finally-in-a-try-except-block"></a>

```python
try:
    file = open('exists.txt')
    print('File exists')
except FileNotFoundError as e:
    print('File does not exist:', e)
except Exception as e:
    print('An error occurred:', e)
else:
    print(file.read())
    file.close()
finally:
    print('This bit always runs. Exception or not.')
```

```
File exists
Here are the file contents.
This bit always runs. Exception or not.
```

```python
try:
    file = open('missing.txt')
    print('File exists')
except FileNotFoundError as e:
    print('File does not exist:', e)
except Exception as e:
    print('An error occurred:', e)
else:
    print(file.read())
    file.close()
finally:
    print('This bit always runs. Exception or not.')
```

```
File does not exist: [Errno 2] No such file or directory: 'missing.txt'
This bit always runs. Exception or not.
```

Matt Clarke, Friday, December 24, 2021

[https://practicaldatascience.co.uk/data-science/how-to-try-except-for-python-exception-handling](https://practicaldatascience.co.uk/data-science/how-to-try-except-for-python-exception-handling)
