Output of Python programs | Set 10 (Exception Handling)
Pre-requisite: Exception Handling in Python
Note: All the programs run on python version 3 and above.
1) What is the output of the following program?
a) Cannot divide by 0 GFG
b) Cannot divide by 0
c) Cannot divide by 0 Inside except block GFG
d) Cannot divide by 0 Inside except block
Explanation: The else block of code is executed only when there occurs no exception in try block.
2) What is the output of the following program?
a) Runtime error
b) Cannot divide by 0 GeeksforGeeks
c) GeeksforGeeks Division successful
Explanation: else block following a finally block is not allowed in python. Python throws syntax error when such format is used.
3) What is the output of the following program?
c) GFG GeeksforGeeks
d) Compilation error
Explanation: At a time only one exception is caught, even though the throw exception in the try block is likely to belong to multiple exception type.
4) What is the output of the following program?
a) GFG ZeroDivisionError GFG ZeroDivisionError
b) GFG ZeroDivisionError Geeks ZeroDivisionError
c) Geeks IndexError GFG ZeroDivisionError Geeks ZeroDivisionError
d) Geeks IndexError GFG ZeroDivisionError
Explanation: finally block of code is always executed whether the exception occurs or not. If exception occurs, the except block is executed first followed by finally block.
5) What is the output of the following program?
a) Compilation error
b) Runtime error
c) GeeksforGeeks GFG Geeks
d) GeeksforGeeks Geeks
Explanation: An else block between finally block between try is defined in python. If there is no exception in try block then else is executed and then the finally block. An except block can be defined to catch multiple exception.
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.