0% found this document useful (0 votes)
279 views

Exception Handing

Uploaded by

Shivani Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
279 views

Exception Handing

Uploaded by

Shivani Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Que1

A) 1. When is ZeroDivisionError exception raised in Python?


II. Give an example code to handle ZeroDivisionError? The code should display the message
"Division by Zero is not allowed" in case of ZeroDivisionError exception, and the message
"Some error occurred in case of any other exception.
Or
(B) I. When is NameError exception raised in Python?
II. Give an example code to handle NameError? The code should display the message "Some
name is not defined in case of Name Error exception, and the message "Some error
occurred" in case of any other exception.
Ans:- A(I) ZeroDivisionError exception is raised whenever a number is divided by zero.
(II) try:
a=int(input("Enter the numerator: "))
b=int(input("Enter the denominator: "))
print("Division is : ".a/b)
except ZeroDivisionError:
print("Division by Zero is not allowed")
except:
print("Some other Error Ocurred")
Or
B(I) NameError is raised when the identifier being accessed is not defined in the local or
global scope.
(II) try:
p= input("Enter a word: ")
print("Replica:", q*3)
except NameError:
print("Some name is not defined")
except:
print("Some Error Ocurred")
Ques-2. (A) (1) Write the uses of try, except, else and finally blocks in brief.
(ii) (a)Which is the base class of all exceptions.
(b) A developer cannot properly maintained the indentation in a program. Which exception
will occur
Or
(B)(1) How many except blocks can be written against a try block.
(II) What will happen if exception handling is not done in a program.
Ans:- 2. A) (i) Exception blocks
try Carries code expected to raise an exception.
* except Carries code to handle / respond against the exception.
* else Carries code that executes if no exception occurs.
* finally: Carries code that executes irrespective of whether exception occurs or not.
(ii) (a) Exception class (b) indentationError
Or
(B) (I) A try block can be written with multiple except blocks. Each except block can
handle a specific type of exception condition and the corresponding code will execute if
the exception occurs.

(II) If exception handling is not done in a code, the program may abnormally close leaving
the user in a confusion.

Que 3. (A) (i) Differentiate else and except blocks in exception handling
(ii) Write a code to open a file readme.txt and print all its contents. Report an error if the
file doesn't exist.
Or
(B) (i) When is a ValueError exception raised in Python.
(ii) Is it compulsory to write the finally block in exception handling.
Ans. 3. (A) (i) except Carries code to handle / respond against the exception.
else Carries code that executes if no exception occurs.
(ii) try:
f1= open ("readme.txt")
str fl.read()
print (str)
fl.close()
except 10Error:
print ("File doesn't Exist")
Or
(B) (i) Raised when a function gets argument of correct type but improper value.
(ii) No, the finally block is optional and carries code that will execute, irrespective of
whether exception occurs or not.
Ques-3. (A) (i) When is the ImportError exception raised?
(ii) Which error is raised by the following code?

import pickle as p
try:
f=open("myfile.dat". "w")
p.dump(["1. "Sachin", "Accts"],"a")
f.close()
except:
print("Module not found")
Or (B) (i) When is ZeroDivisionError exception raised?
(ii) When is the code in else block executed?
Ans:- (A) (i) Import error is raised if the module being imported is not found.
(ii) ImportError
Or
(B) (i) ZeroDivisionError is Raised the code divides a number by a zero denominator.
(ii) No, the finally block is optional and carries code that will execute, irrespective of
whether exception occurs or not.
Ques-4.(A) (i) What is the use of except clause in exception handling?
(ii) Which error is raised by the following code:
import pickle as p
a=20
b=5
try:
if a/b > 4:
print(a/b)
else:
print((a+b)//2)
except:
print("Error in code ")
Or
(B) (i) When is KeyError exception raised.
(ii) When is IOError exception raised.
Ans:- 4. A) (i) The except clause houses code that will catch the exceptions and execute
proper exception handling code, if an exception occurs.
(ii) IndentationError
Or
(B) (i) KeyError is raised when the key to be accessed is not found in the dictionary.
(ii) IOError exception occurs if Input/Output operation fails.

Ques – 5. (A) (i) What are Semantic errors. Give an example.


(ii) What output will the following code produce:
try:
filel-open("Dairy.txt","r")
try:
filel.write("Python Programming")
finally:
filel.close()
print("Diary file closed")
except:
print("Error")
Or
(B) (i) Write the different types of errors in a program. Which errors are difficult to find and
why?
(ii) What output will the following code produce:
try:
d("Eid":09. "Ename": "Rina". "Dept":"Accts")
print(d["Salary"])
finally:
print("Code ends")
except KeyError:
print("Key not found")
Ans 5 : (A) (i) Semantic errors are the errors that happen due to writing of meaning less
statements in a program.
Example : x+y=60
(ii) Output:
Diary file closed
Error
Or
(B) (i) Different types of errors in a program are:
(a) Syntax error (b) Semantic error (c) Type error (d) Run time error (e)
Logical error.
Logical and Run time errors are more difficult to detect and rectify.
(ii) Key not found
Code ends.
Ques 6: (A) (i) What are compile time errors. Give an example.
(ii) What output will the following code produce: if x=10, y=0
try:
n1=int(input("Enter a number:"))
n2-int(input("Enter a number"))
print(n1//n2)
except:
print("Some Error occured")
else:
print("No Erroг")
OR
(B) (i) What are run time errors. Give an example.
(ii) What output will the following code produce: if x-100, y=10
try:
n1=int(input("Enter a number:"))
n2=int(input("Enter a number: "))
print(n1//n2)
except:
print("Some Error occured")
else:
print("No Error")
Ans:- 32. (A) (i) Compile time errors are the errors that are usually detected during
program compilation and can be easily detected.
Example: Syntax error, Semantic error.
(ii) Output: Some Error occured
Or
(B) (i) Run time errors are the one that occur during program run and are difficult to detect.
Example: Hardware error, File not found etc.
(ii) Output: 10
No Error
Ques 7: A) (i) Why is it necessary to handle exceptions in a program?
(ii) What output will the following code produce, if the file "t1.txt" is not present
try:
f-open("tl.txt","r")
s-f.read()
print(s)
except FileNotFoundError:
print("File does not exist")
else:
print("File successfully processed")
finally:
print("File handling code ends")
Or
(B) (i) How many except blocks can be written against a try block?
(ii) What output will the following code produce:
try:
f-open("tl.txt"."w")
f.write("AISSCE2025)
f.close()
f-open("tl.txt","r")
s-f.read()
print(s)
except FileNotFoundError:
print("File does not exist')
else:
print("File successfully processed")
finally:
print("File handling code ends')
Ans:- (A) (i) Handling exceptions in a code creates a safe or guard code that has lesser
probabilities to abnormally terminate.
(ii) Output:
File does not exist
File handling code ends
Or
(B) (i) Multiple except blocks can be written against a try to match and handle different
kinds of exceptions.
(ii) Output:
AISSCE2025
File successfully processed
File handling code ends
Ques 8:-

You might also like