Output of Python programs | Set 10 (Exception Handling)

10 Feb, 2020
Pre-requisite: Exception Handling in PythonNote: All the programs run on python version 3 and above.
1) What is the output of the following program?
data = 50try:    data = data/0except ZeroDivisionError:    print('Cannot divide by 0 ', end = '')else:    print('Division successful ', end = '')  try:    data = data/5except:    print('Inside except block ', end = '')else:    print('GFG', end = '')
a) Cannot divide by 0 GFGb) Cannot divide by 0c) Cannot divide by 0 Inside except block GFGd) Cannot divide by 0 Inside except block
Ans. (a)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?
data = 50try:    data = data/10except ZeroDivisionError:    print('Cannot divide by 0 ', end = '')finally:    print('GeeksforGeeks ', end = '')else:    print('Division successful ', end = '')
a) Runtime errorb) Cannot divide by 0 GeeksforGeeksc) GeeksforGeeks Division successfuld) GeeksforGeeks
Ans. (a)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?
value = [1, 2, 3, 4]data = 0try:    data = value[4]except IndexError:    print('GFG', end = '')except:    print('GeeksforGeeks ', end = '')
a) GeeksforGeeksb) GFGc) GFG GeeksforGeeksd) Compilation error
Ans. (b)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?
value = [1, 2, 3, 4]data = 0try:    data = value[3]except IndexError:    print('GFG IndexError  ', end = '')except:    print('GeeksforGeeks IndexError  ', end = '')finally:    print('Geeks IndexError  ', end = '')  data = 10try:    data = data/0except ZeroDivisionError:    print('GFG ZeroDivisionError  ', end = '')finally:    print('Geeks ZeroDivisionError  ')
a) GFG ZeroDivisionError GFG ZeroDivisionErrorb) GFG ZeroDivisionError Geeks ZeroDivisionErrorc) Geeks IndexError GFG ZeroDivisionError Geeks ZeroDivisionErrord) Geeks IndexError GFG ZeroDivisionError
Ans. (c)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?
value = [1, 2, 3, 4, 5]try:    value = value[5]/0except (IndexError, ZeroDivisionError):    print('GeeksforGeeks ', end = '')else:    print('GFG ', end = '')finally:    print('Geeks ', end = '')
a) Compilation errorb) Runtime errorc) GeeksforGeeks GFG Geeksd) GeeksforGeeks Geeks
Ans. (d)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 contribute@geeksforgeeks.org. 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.
chaudhary_19
Python-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.