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
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
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
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
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
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
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
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
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
What types of exceptions should you catch?
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
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

Continue exploring

Profile picture of Trey

Learn something new about Python

My name is Trey Hunner. I publish new Python articles and screencasts through Python Morsels. If you want to keep learning new things about Python, join Python Morsels!

Join Python Morsels ✨