Raise Exception, Rest Still Runs | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Raise Exception, Rest Still Runs

This code from a lesson runs fine. try: print("Hello") print(1 / 0) except ZeroDivisionError: print("Divided by zero") # this is where it should stop and the following line should not run, but it does print("This code will run no matter what") I deletes "finally" and made the print() line it's own code... everything runs. Why doesn't the program stop running after an exception is called?

30th Mar 2017, 3:52 AM
James Bradley
James Bradley - avatar
5 Answers
+ 18
I believe this is what we call exception handling in Python (?). The exception is caught, the process for that block is terminated and a corresponding prompt is sent to console output. Exception handling enables the program to continue to function without crashing.
30th Mar 2017, 4:59 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Oh okay. wow even if this IS simple it's still a lot to take in.
30th Mar 2017, 5:01 AM
James Bradley
James Bradley - avatar
+ 3
Exceptions aren't crashes. It just means that if A doesn't work, do B.
30th Mar 2017, 6:59 PM
Jeremie
Jeremie - avatar
+ 1
I believe the print is executed due to Indentation factor. It is in same line as try and except blocks defined.
30th Mar 2017, 11:44 AM
Avinash D
Avinash D - avatar
+ 1
yes, but the last line containing "print" was a "finally" block. i erased "finally" and made print it's own line. i was wondering why it didn't stop at the error.
30th Mar 2017, 7:01 PM
James Bradley
James Bradley - avatar