Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
156 views
in Computer by (49.6k points)
closed by

Catching Exceptions.

1 Answer

+1 vote
by (51.9k points)
selected by
 
Best answer

An exception is said to be caught when a code that is designed to handle a particular exception is executed. Exceptions, if any, are caught in the try block and handled in the except block. While writing or debugging a program, a user might doubt an exception to occur in a particular part of the code. Such suspicious lines of codes are put inside a try block. Every try block is followed by an except block. The appropriate code to handle each of the possible exceptions (in the code inside the try block) are written inside the except clause. 

While executing the program, if an exception is encountered, further execution of the code inside the try block is stopped and the control is transferred to the except block. 

The syntax of try … except clause is as follows:

try: 

  [ program statements where exceptions might occur] 

except [exception-name]: 

  [ code for exception handling if the exception-name error is

encountered]

Program : Using try..except block

print ("Practicing for try block") 

try: 

   numerator=50 

   denom=int(input("Enter the denominator")) 

   quotient=(numerator/denom) 

   print(quotient) 

   print ("Division performed successfully") 

except ZeroDivisionError: 

   print ("Denominator as ZERO.... not allowed") 

print(“OUTSIDE try..except block”)

In Program, the ZeroDivisionError exception is handled. If the user enters any non-zero value as denominator, the quotient will be displayed along with the message “Division performed successfully”, as shown in Figure. The except clause will be skipped in this case. So, the next statement after the try..except block is executed and the message “OUTSIDE try.. except block” is displayed.

However, if the user enters the value of denom as zero (0), then the execution of the try block will stop. The control will shift to the except block and the message “Denominator as Zero…. not allowed” will be displayed, as shown in Figure. Thereafter, the statement following the try..except block is executed and the message “OUTSIDE try..except block” is displayed in this case also.

Output without an error

Output with exception raised

Sometimes, a single piece of code might be suspected to have more than one type of error. For handling such situations, we can have multiple except blocks for a single try block as shown in the Program.

Program : Use of multiple except clauses

print ("Handling multiple exceptions") 

try: 

   numerator=50 

   denom=int(input("Enter the denominator: ")) 

   print (numerator/denom) 

   print ("Division performed successfully") 

except ZeroDivisionError: 

   print ("Denominator as ZERO is not allowed") 

except ValueError: 

print ("Only INTEGERS should be entered")

In the code, two types of exceptions (ZeroDivisionError and ValueError) are handled using two except blocks for a single try block. When an exception is raised, a search for the matching except block is made till it is handled. If no match is found, then the program terminates. 

However, if an exception is raised for which no handler is created by the programmer, then such an exception can be handled by adding an except clause without specifying any exception. This except clause should be added as the last clause of the try..except block. The Program given below along with the output given in Figure. explains this.

Program : Use of except without specifying an exception

print ("Handling exceptions without naming them") 

try: 

   numerator=50 

   denom=int(input("Enter the denominator")) 

   quotient=(numerator/denom) 

   print ("Division performed successfully") 

except ValueError: 

   print ("Only INTEGERS should be entered") 

except: 

   print(" OOPS.....SOME EXCEPTION RAISED")

If the above code is executed, and the denominator entered is 0 (zero) , the handler for ZeroDivisionError exception will be searched. Since it is not present, the last except clause (without any specified exception) will be executed , so the message “ OOPS.....SOME EXCEPTION RAISED” will be displayed.

Output of Program

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...