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

Exception Handling

Uploaded by

shashwanth017
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Exception Handling

Uploaded by

shashwanth017
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

ERRORS IN PYTHON AND DEBUGGING:

 The process of finding errors in a program is termed as


Debugging.

 Due to errors, a program may not execute or may


generate wrong output. So, it becomes necessary to find
and remove errors for successful execution of a program
Error
Syntax
sRUN TIME LOGICAL
ERROR ERROR ER
ROR
Syntax
ERROR
 These types of errors are generated when we violate the syntax or, in other words, the
grammatical rules of a programming language.
 Some examples of Python syntax errors are:
1. Incorrect indentation
2. Misspelling a keyword
3. Leaving out a symbol such as colon (:),
comma (,) or parentheses (())
Logical
ERROR
 A logical error/bug (called semantic error) does not stop execution but the program behaves
incorrectly and produces undesired/wrong output.
Some examples of Python syntax errors are:
1. Using the wrong variable name for calculations.
2. Using integer division or modulus operator in place of division operator
3. Giving wrong operator precedence.
Exception
Handling
Exceptio
n

An exception is an Whenever there is


error that occurs an error, Python
during the generates an
exception that
execution of the could be handled
program.
Erro Exceptio
r v n
Exceptio
Error s n
Any Unexpecte
bug in d
situation
the during
code program
execution
Handling the run time errors is known as Exception
Handling
TO HANDLE THE EXCEPTION

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

Method 2 try...except with else block

Method 3 try with multiple except


blocks
Method 4 try...except with finally
block
Program without exception handling
Program to divide two
numbers
Program with exception handling
Program to divide two
numbers
Handling multiple exceptions
Program to divide two
numbers
Exception handling – Execution
order
The named except: blocks
handle the named exceptions
while the unnamed except:
block handles all other
exceptions – exceptions not
mentioned in named except:
blocks.
The finally
block
• The finally: block is a place that contains any
code that must execute, whether the try: block
raised an exception or not.
• the except: block will get executed only in case
an exception is raised and finally: block will get
executed ALWAYS, in the end.
finally block
Raising/Forcing an
Exception
• Exceptions can be raised forcefully
• Use raise keyword
• Syntax: raise <exception>
(<message>)
• Must be pre-defined Built-in
exception.
Benefits of Exception
Handling
• Exception handling separates error-handling
code from normal code.
• It clarifies the code and enhances readability.
• It stimulates consequences as the error-
handling takes place at one place and in one
manner.
• It makes for clear, robust, fault-tolerant
programs.
seek(
) the position of the file pointer
To change
to a given specific position

File pointer is like a cursor, which defines


from where the data has to be read or
written in the file

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

File pointer will be at the beginning of the file in w and r mode


Current position of the file pointer in ‘a’ mode

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

You might also like