Exceptions

Exceptions happens! When exceptions happen, how should interpret the traceback for an exception? And how, when, and where should you catch exceptions?

10 articles · 56 min read
8 screencasts · 28 min watch
01
Deciphering Python's Traceback (most recent call last)
5 min read Screencast available

When exceptions go unhandled, Python prints a traceback. Tracebacks are read from the bottom upward. The last line describes what happened and lines above describe where it happened.

Read
Read
02
Python's try-except blocks
4 min read Screencast available

Is your Python program printing out a traceback? You have an uncaught exception! You can catch that exception with a try-except block.

Read
Read
03
How to raise an exception
5 min read Screencast available

Have a specific condition that should loudly crash your program? Use Python's raise statement with an exception object (make sure to provide a helpful error message) to raise an exception!

Read
Read
04
SyntaxError: invalid syntax
14 min read

Python's "invalid syntax" error message comes up often, especially when you're first learning Python. What usually causes this error and how can you fix it?

Read
Read
05
Fixing TypeError: can only concatenate str (not "int") to str
5 min read

Python crashed with the error TypeError: can only concatenate str (not "int") to str. Essentially Python's saying you've used + between a string and a number and it's unhappy about it. Let's talk about how to fix this issue and how to avoid it more generally.

Read
Read
06
What can you do with exception objects?
2 min read Screencast available Premium

Handling an exception and need to access the exception object? Use the as keyword in your except block.

Read
Read
07
Catching multiple exception types
6 min read Screencast available Premium

Need to handle multiple types of exceptions at once? Python has a few different ways to catch multiple exception types at once.

Read
Read
08
Catching all exceptions
4 min read Screencast available Premium

You can catch all exceptions implicitly, but it's usually best to be explicit (even when catching all exceptions).

Read
Read
09
What types of exceptions should you catch?New
5 min read Screencast available

The trickiest programming bugs are often caused by catching exceptions that you didn't mean to catch or handling exceptions in ways that obfuscate the actual error that's occurring. Which exceptions should you catch and which should you leave unhandled?

Read
Read
10
Re-raising exceptions
6 min read Screencast available Premium

Re-raising exceptions in Python can be used to log exceptions for troubleshooting, to chain exceptions for clearer context, and to simplify tracebacks by suppressing the original exception.

Read
Read

Continue exploring

Profile picture of Trey

Learn something new about Python every week

My name is Trey Hunner. I publish new Python articles and screencasts every week through Python Morsels. If you want to learn something new about Python every week, join Python Morsels!

Join Python Morsels ✨