Exception Handling in Python
Class 12 · Computer Science · 8 Questions
“Every syntax error is an exception but every exception cannot be a syntax error.” Justify the statement.
When are the following built-in exceptions raised? Give examples to support your answers.
What is the use of a raise statement? Write a code to accept two numbers and display the quotient. Appropriate exception should be raised if the user enters the second number (denominator) as zero (0).
Use assert statement in Question No. 3 to test the division expression in the program.
Define the following:
Explain catching exceptions using try and except block.
Consider the code given below and fill in the blanks. print ("Learning Exceptions...") try: num1= int(input ("Enter the first number")) num2=int(input("Enter the second number")) quotient=(num1/num2) print ("Both the numbers entered were correct") except _____: # to enter only integers print ("Please enter only numbers") except _____: # Denominator should not be zero print("Number 2 should not be zero") else: print("Great .. you are a good programmer") _____: # to be executed at the end print("JOB OVER... GO GET SOME REST")
What is the use of finally clause? Use finally clause in the problem given in Question No. 7.