Using raise for Effective Exceptions (Summary)
In your Python journey, you’ll come across situations where you need to signal that something is going wrong in your code. For example, maybe a file doesn’t exist, a network or database connection fails, or your code gets invalid input. A common approach to tackle these issues is to raise an exception, notifying the user that an error has occurred. That’s what Python’s raise
statement is for.
Learning about the raise
statement allows you to effectively handle errors and exceptional situations in your code. This way, you’ll develop more robust programs and higher-quality code.
In this video course, you’ll learn how to:
- Raise exceptions in Python using the
raise
statement - Decide which exceptions to raise and when to raise them in your code
- Explore common use cases for raising exceptions in Python
- Apply best practices for raising exceptions in your Python code
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 In the previous lesson, I explained exception groups. In this lesson, I’ll summarize the course. Exceptions give you a way of interrupting the flow of your code and typically get used for error processing.
00:14
Exceptions are objects and inherit from the Exception
class, and you can write custom exceptions simply by creating your own class, which inherits from this as well.
00:26
When handling an exception, you can use the raise
keyword to re-raise the same exception, or you can raise a different exception instead. By using the from
keyword, you can chain exceptions together or bury the original exception by raising from None
.
00:45
Finally, you learned about the new ExceptionGroup
feature added in Python 3.11. This allows you to raise a collection of exceptions and you use the except*
syntax to handle a single exception from within a group.
01:01 That’s all for me. I hope you found this course useful. Thanks.
Michael Gaunt on April 16, 2024
Exceptions groups look cool!
![Avatar image for Christopher Trudeau](/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/headshot_alt_crop.3e4f0b089285.jpg)
Christopher Trudeau RP Team on April 17, 2024
Thanks folks, glad you enjoyed the course.
Yep, I find I use built-in exceptions the majority of the time. I only tend to create custom ones if I truly need to express something original.
As for the groups, yeah, they’re neat. I’ve yet to use them in real life, but handy to have when the need arises.
Become a Member to join the conversation.
rjleon on April 16, 2024
Great tutorial and lots of context and practicality here. What hit home for my current project is to only use exceptions to convey a better message than what you get by default. I always wondered why I’m using a custom exception when the default messaging was already clear? I went back through my code and removed my custom exceptions.