Exception Handling
Exception Handling
When an exception occurs in the program, we say that exception was raised or thrown. Next,
we deal with it and say it is handled or caught. And the code written to handle it is known as
exception handler.
For handling exceptional situations Python provides—
1. raise statement to raise exception in the program.
2. try... except statement for catching and
handling errors (which will be taken up
in ‘Handling Exceptions in Python’ section).
Syntax:
raise [exception name [, message/argument][, traceback]]
Common Python Exc
Methods to handle the exception
Method 1 try and except block
f.seek(file-location)
f.seek(offset, from_what)
0 1 2
• Sets the • Sets the • Sets the
reference reference
point at point at referenc
the the e at
beginning current the end
of the file of the
file, position file
which is
by
tell( ) Tells the current position
of the file pointer
DemoFile.tx
t
Progra
m
Outpu
t
Demofile.txt
Current position of the file pointer in ‘r’/’w’ mode
Output
Output
File pointer will be at the end of the file in append ‘a’ mode
Moving the position of file pointer using seek()
Output
Output
JINI N K