Logo

Exceptions

Exceptions and Raising Errors

Learning Outcomes

  • Using Try/Except Blocks
  • Raising Exceptions
  • Understanding different types of exceptions

Exceptions

As you have likely seen, writing code without errors is hard. To help with errors, we can use methods of exception handling to direct our program when an error has occurred such as using the try/except structure and raising errors. Python errors return a traceback, which is information about the error such as the error message, the line number where the error occurred, and the sequence of functions that led to that error.


Try/Except Block

A common error we may face is opening a file that does not exist. A common way to troubleshoot such a problem would be the use of the try/except block.

try:
  file = open('data.txt')
except Exception:
  print("File doesn't exist")

Here, all code inside of the try block gets executed unless it finds an error. If there is an error, the except block will catch and run the code inside of its block. We can add an else statement whose code will get executed if there is no exception.

try:
  file = open('data.txt')
except Exception:
  print("File doesn't exist")
else:
  file.close()

We may also want to run a piece of code whether there was an exception or not. This can be achieved with the help of the finally statement.

try:
  file = open('data.txt')
except Exception:
  print("File doesn't exist")
else:
  file.close()
finally:
  print('done')

So far, our try/except block only checks whether there is an exception to decide which blocks to run. We can find the specific exception we are catching by printing the error. We can do so with the following:

try:
  file = open('data.txt')
except Exception as e:
    print('Error: ' + str(e))
else:
  file.close()

If our file does not exist, we are likely to receive the following error:

Error: [Errno 2] No such file or directory: 'data.txt'

This is more descriptive, but we can still do better. If we run our code outside of a try/except block with a file that does not exist, we will get a FileNotFoundError. That means we can catch this specific error in an extra except statement.

try:
  file = open('data.txt')
except FileNotFoundError as e:
  print("File doesn't exist: " + str(e))
except Exception as e:
    print('Error: ' + str(e))
else:
  file.close()

Raising Errors

Just as we could catch errors in our except statements, we can also raise exceptions if our code behaves a particular way. To do so, we can use the raise keyword along with a helpful message passed into our exception.

number = 'Hello'
if type(number) != int:
    raise Exception('Not a number')

We could also raise a specific error, like a TypeError for this line of code.

number = 'Hello'
if type(number) != int:
    raise TypeError('Not a number')

Remember to raise errors with caution, if they are raised outside of a try/except block they will crash your program.


Knowledge Check

  • When would you use the finally statement in a try/except block? How about the else statement?
  • Which error is raised if we try to open a file that does not exist?
  • How can we print the error to the console when using the try/except block?

Additional Resources

Contribute to this lesson