Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Else, Finally & Exceptions Recap

In this lesson, you’ll learn how to use the else and finally keywords when handling exceptions. You’ll see that else clauses only get run if no exceptions occurred and finally clauses get run whether there were exceptions or not, making them useful for tidying up when programs fail unexpectedly.

00:01 Next, you’ll look at the else clause. As you can see here, the else clause comes after the try and except block, and that code is run if no exceptions have occurred. Here you can see the previous program. And now an else clause is going to be added, which is going to say 'thanks for putting in a number'.

00:28 So now you’ll see what happens when the value of x is changed to something that doesn’t generate an exception, such as 1. And then run the program, and you can see the else block is run and the print statement with 'thanks for putting in a number' is executed.

00:48 Next, you’re going to look at using finally. As you can see onscreen, finally comes after all the other blocks we’ve already seen, although an else doesn’t necessarily need to be present.

01:00 finally ensures that code gets run regardless of whether any exceptions have occurred or not. This can often be useful for cleaning up when you’ve opened files, et cetera.

01:11 To keep it simple, you’ll see that simulated with a couple of print statements. So here you have

01:21 opening a file and then at the end,

01:30 the file will be closed. At the moment this will run because we have a sensible value for x. We can see at the beginning we would have opened a file, done some work, closed the file at the end. However, you may have noticed that there’s only one kind of exception which had been caught here, and that’s just the ZeroDivisionError.

02:01 If the program’s run now, you can see if the file is opened and the exception is generated, the exception is not being handled and the file is being left open, which is undesirable.

02:16 Here’s where finally comes into play to ensure the file gets closed by moving that action inside the block. Now you’ll see that running and now you can see that the file is opened and the file is closed even when that exception is generated.

02:40 So that’s when finally blocks can be useful—by tidying up even when a program fails. So, there you have it! You’ve reached the end of this introduction to Python exceptions.

02:53 You’ve seen the difference between exceptions which occur in running code and syntax errors which are detected before the code runs. You’ve seen how to raise an exception with the raise keyword.

03:05 You’ve seen the AssertionError exception, which occurs when using the assert keyword to test for a condition being True.

03:13 You’ve seen how to handle exceptions in running programs using the try and except blocks. You’ve seen the else clause, which allows you to run code when an exception didn’t occur.

03:25 And you’ve seen finally, allowing you to run code which cleans up regardless of whether any exception’s happened or not. I hope you’ve enjoyed this series of videos and we’ll see you again soon at Real Python.

Avatar image for Robert

Robert on March 14, 2019

Very nice, I’ll keep watching

Avatar image for Anonymous

Anonymous on March 15, 2019

Thanks for f'text{var}'! Previously I used 'text{}'.format(var) and it was messy when a lot of variables need to be provided.

Avatar image for Paul

Paul on March 15, 2019

Well done. Good, clear, concise overview.

Avatar image for Anonymous

Anonymous on March 15, 2019

Really helpful! I can use what I learned today!

Avatar image for Anonymous

Anonymous on March 16, 2019

Very helpful. Straight to the point!

Avatar image for Daniel

Daniel on March 18, 2019

Great videos. Very helpful.

Avatar image for Blaise Pabon

Blaise Pabon on March 19, 2019

Now I realize that I never really understood try, catch, finally.

Avatar image for Chaitanya

Chaitanya on March 21, 2019

A nice way of explaining the introduction part about try, except, else, finally

Avatar image for andrewcheryl

andrewcheryl on March 26, 2019

Super helpful and clear!

Avatar image for rklyba

rklyba on July 5, 2019

Thank you.It’s very simple and clear. Now I gonna read “Python Exceptions: An Introduction”

Avatar image for Tatchi Wiggers

Tatchi Wiggers on Sept. 4, 2019

Excellent!! Thank you!

Avatar image for DanielHao5

DanielHao5 on Nov. 26, 2019

It’s short and sweet. Wish it can be expanded with some more examples… next serise.

Avatar image for SyedRehanAhmed

SyedRehanAhmed on Dec. 7, 2019

Do you have quiz for exceptions ?

Avatar image for kihntonyk

kihntonyk on Feb. 9, 2020

Great presentation. Thank you.

Avatar image for DaveLyu

DaveLyu on Feb. 11, 2020

Great presentation. Thank you.

Avatar image for isayahc

isayahc on May 18, 2020

Awesome tutorial!

Avatar image for JulianV

JulianV on May 31, 2020

A very bioled down intro. Thanks.

Avatar image for Carlo Barrientos

Carlo Barrientos on May 31, 2020

Simple, clear, concise, covers all basics. Uses simple examples along with large font with bpython syntax-aware color-coded fonts. This is incredibly easy to follow especially for the topic of Exception handlers because many other tutorials go far beyond “the basics” and thus lose the audience. I hope to see more from Darren in future. BTW: I would like “some mention” of how Exceptions down deep in the call stack can percolate up to the top main

Avatar image for Ghani

Ghani on Oct. 24, 2020

Good course; thank you!

Avatar image for Abderraouf Z

Abderraouf Z on Nov. 21, 2020

Great course! Very good explanations.

Avatar image for Robert T Paulsen

Robert T Paulsen on Nov. 24, 2020

I echo all the above comments - nice job!

Avatar image for L Pedigo

L Pedigo on April 20, 2021

This is a good lesson and I understood the material well once finished. I had been watching/reading other courses on this subject (try, except, assert) and was still not clear on how to use them. This course was way easier to understand! Very well structured and presented. Thanks for your efforts!

Avatar image for Kumaran Ramalingam

Kumaran Ramalingam on June 13, 2021

Thank you so much Darren Jones: I believe in learning basics to the core would be the first best step we have to do on any technology. once we know it we can crack it. I am an Automation Developer working on infrastructure layer required more Error handlings and exceptions on case to case. Thanks for making me easily understand this tricky concept in Python. I will start from here.

Avatar image for Ricardo Joseh Lima

Ricardo Joseh Lima on Aug. 18, 2021

Great lesson, simple, direct, congratulations!

Avatar image for mihail-t

mihail-t on Nov. 30, 2024

It would be good to add an example of ‘raise … from …’

Become a Member to join the conversation.