0% found this document useful (0 votes)
27 views2 pages

Exception Handaling

to take knowledge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Exception Handaling

to take knowledge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

>>> print("hello') ModuleNotFoundError: No module named 'cs'

SyntaxError: EOL while scanning string literal >>> x=*0+*10000000000


>>> 5000*10*2/1000 Traceback (most recent call last):
100.0 File "<pyshell#17>", line 1, in <module>
>>> 10/'raj' x=*0+*10000000000
Traceback (most recent call last): MemoryError
File "<pyshell#4>", line 1, in <module> >>> with open('raj.txt','r')
10/'raj' SyntaxError: invalid syntax
TypeError: unsupported operand type(s) for /: 'int' and >>> with open('raj.txt','r'):
'str' content=x.read()
>>> int('raj') Traceback (most recent call last):
Traceback (most recent call last): File "<pyshell#21>", line 1, in <module>
File "<pyshell#6>", line 1, in <module> with open('raj.txt','r'):
int('raj') FileNotFoundError: *Errno 2+ No such file or directory:
ValueError: invalid literal for int() with base 10: 'raj' 'raj.txt'
>>> a=10 try:
>>> print(b) a='raj'
Traceback (most recent call last): print(int(a))
File "<pyshell#8>", line 1, in <module> except Exception as e:
print(b) print(e)
NameError: name 'b' is not defined invalid literal for int() with base 10: 'raj'
>>> x=*1,2,3+ try:
>>> print(x*3+) a='raj'
Traceback (most recent call last): print(b)
File "<pyshell#10>", line 1, in <module> except Exception as e:
print(x*3+) print(e)
IndexError: list index out of range name 'b' is not defined
>>> x='raj' try:
>>> print(x.capital()) print(5/0)
Traceback (most recent call last): except Exception as e:
File "<pyshell#12>", line 1, in <module> print(e)
print(x.capital()) division by zero
AttributeError: 'str' object has no attribute 'capital' '''An exception is a Python object that represents an
>>> x=,1:10,2:20- error.
>>> print(x*3+) When an error occurs during the execution of a
Traceback (most recent call last): program,
File "<pyshell#14>", line 1, in <module> an exception is said to have been raised.
print(x*3+) Such an exception needs to be handled by the
KeyError: 3 programmer
>>> 5/0 so that the program does not terminate abnormally.
Traceback (most recent call last): Therefore, while designing a program, a programmer
File "<pyshell#15>", line 1, in <module> may anticipate such erroneous situations that may
5/0 arise
ZeroDivisionError: division by zero during its execution and can address them by
>>> import cs including
Traceback (most recent call last): appropriate code to handle that exception.'''
File "<pyshell#16>", line 1, in <module> #Built-in exceptions in Python
import cs '''
#1. ValueError It is raised when a built-in method or print ("Division performed successfully")
operation receives an argument that has the right data except ZeroDivisionError:
type but mismatched or inappropriate values. print ("Denominator as ZERO is not allowed")
a=5 #6. IndexError It is raised when the index or subscript
b='x' in a sequence is out of range.
try: a = *1, 2, 3+
c=int(a)+int(b) try:
print(c) print("Second element =" ,(a*1+))
except ValueError: #throws error since there are only 3 elements in
print("Only numbers should be required.") array
#2. IOError It is raised when the file specified in a print("Fourth element =" ,(a*3+))
program statement cannot be opened. except IndexError:
try: print("An error occurred out of range.")
f=open("testfile.txt","r") #7. NameError It is raised when a local or global
f.write("This is my test file for exception handling.") variable name is not defined.
except IOError: try:
print("Input Output Error: you can't write data in print(x)
file.") except NameError:
else: print("Variable x is not defined")
print("Written content in the file successfully") #8. IndentationError It is raised due to incorrect
f.close() indentation in the program code.
#3. EOFError It is raised when the end of file condition try:
is reached without reading any data by input(). marks = 35
import pickle as p if marks > 33:
f=open('D:\Teaching Aids\XII\student1.dat','rb') print('PASS')
while True: except IndentationError:
try: print ("incorrect indentation.")
row=p.load(f) #9. If the condition is true, nothing happens. But if the
print(row) condition is false, AssertionError is raised.
except EOFError: a=6
print ("The pointer is not be reached the end of try:
file.") assert a < 5
#break except AssertionError:
f.close() print('Given the condition is false.')
#4. TypeError It is raised when an operator is supplied #10.
with a value of incorrect data type. try:
try: x=50
a=5 y=int(input("Enter the denominator: "))
b='0' z=(x/y)
print (a+b) print ("Division performed successfully.")
except TypeError: except ZeroDivisionError:
print('Given value is incorrect.') print ("Denominator as ZERO is not allowed.")
#5. ZeroDivisionError It is raised when the except ValueError:
denominator in a division operation is zero. print ("Only INTEGERS should be entered.")
try: else:
x = 50 print ("The result of division operation is ", z)
y=0 finally:
z=x/y print ("Thanking for using Exception handling.")

You might also like