Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Raising and Handling Python Exceptions (Summary)

After seeing the difference between syntax errors and exceptions, you learned about various ways to raise, catch, and handle exceptions in Python.

In this course, you explored how to:

  • Differentiate exceptions from syntax errors
  • Use the assert keyword to check if a certain condition is met
  • Write custom exceptions that subclass Exception
  • Catch exceptions with a tryexcept block
  • Use the additional keywords else and finally
  • Catch built-in exceptions

Sample Code (.zip)

1.7 KB

Course Slides (.pdf)

1.5 MB

00:00 You made it all the way to this final summary lesson. Throughout the course, you encountered a couple of keywords. You learned about the assert keyword, the raise keyword, and then about tryexcept blocks with the two optional else and finally keywords.

00:16 You started off by learning the difference between an exception and a syntax error. Remember that exceptions are errors that happen during runtime and syntax errors are errors that happen during parsing.

00:28 You learned how to raise an exception using the raise keyword and then passing an exception object in there, whether that’s one that you defined yourself or it’s one of the built-in ones.

00:37 Then you learned that you can use the assert keyword for quick checks during development for asserting that a certain condition is true, and if it isn’t, then you want to raise an AssertionError with a message that you can pass.

00:49 You also learned that you don’t want to use this assert keyword if there’s any chance that you might want to optimize your code at some point because then any of these assertions are just going to be skipped.

01:00 Then you learned about catching exceptions that you or Python raise for you using the tryexcept block, which is a common construct that you will see around Python code and most likely the most common way that you’re going to write exception handling in your Python code.

01:14 It works the way that you add some code in the try block that you want to try, and if an exception occurs, you want to be specific and catch that exception and then execute some other code instead of ending your program at that point.

01:28 Then you also learned how you can handle the success case with the optional else keyword that you can add to a tryexcept block and that includes some code that is only going to execute if no exception was raised at all.

01:40 You learned how to catch a built-in exception, similar to how you caught one of your custom exceptions before just as a training. You also saw that there’s a lot of built-in exceptions and that you can pick from those and make sure that the flow of your program takes them into account.

01:56 And finally, you learned about how you can clean up at the end of a tryexcept block with the optional finally keyword that opens up a code block where you can write some code that is going to execute whether or not an exception was raised earlier on. So it’s just going to execute anyways and you can use it to, for example, close some file streams or add some additional logging to your application.

02:21 And that’s all for this intro to exceptions. Thanks for joining me here. My name is Martin, and I hope you have a nice day and I’ll see you around at Real Python!

Avatar image for aniketbarphe

aniketbarphe on Dec. 25, 2021

Thank You!

Avatar image for SamR

SamR on Dec. 29, 2021

Very helpful. Thanks!

Become a Member to join the conversation.