+ 2

Why is the print statement of print (hello) missed in this code

try: print(1 / 0) print("Hello") except ZeroDivisionError: print("Divided by zero") finally: print("This code will run no matter what")

30th May 2018, 10:31 PM
Chris Sheffield
Chris Sheffield - avatar
3 odpowiedzi
+ 2
Thank you Faisal
30th May 2018, 10:46 PM
Chris Sheffield
Chris Sheffield - avatar
+ 2
Because you put print("hello") in try block which raises ZeroDivisionError and jump to catch block.
31st May 2018, 8:49 AM
Ritesh Singh
Ritesh Singh - avatar
+ 1
Because 1 ÷ 0 raises the ZeroDivisonError and the print statement that outputs hello is after the print that raises the error, once the error is raised, everything else within the try statement gets skipped over as the code will jump straight to the except statement. For "Hello" to be printed, it would need to appear before the first print to avoid getting it ignored.
30th May 2018, 10:44 PM
Faisal
Faisal - avatar