Exception Handling
Exception Handling
What is Exception?
• An exception is an event, which occurs during the execution of a
program that disrupts the normal flow of the program’s instructions.
• In general, when a Python script encounters a situation that it cannot
cope with, it raises an exception.
• An exception is a Python object that represents an error.
• When a Python script (code) raises an exception, it must either handle
the exception immediately otherwise it terminates and quits.
Syntax Error vs Runtime Error
• Syntax Error are those error which is occurred due to the invalid syntax.
Code:
Runtime Errors:
• Runtime error are those error which is occurred at runtime(execution time).
• Code was syntactically correct, but the error occurs when the program is
executing.
• Runtime Error is also known as exception.
Code:
• If the value of b is 0.
• If the file ‘soft.txt’ is not
• If we input value ‘ten’ instead
available
of 10.
Runtime Error
?
?
What is
Exception?
=
Points to remember:
• Objectives:
• In order to stop the abnormal flow of execution.
• Providing user-friendly message.
• Continuing rest of program.
Think:
• What are the errors that you get when an exception occur?
• From where they are coming?
• Python has many built-in exceptions which forces your program to output an
error when something in it goes wrong.
Exception Hierarchy in Python:
PVM
Execute Match the
exception and
make the object of
Enter first num: 5 exception : throw
Enter the second num:0 that in your
program
Line 3 c = a/b
[Error occurred: cannot dived by 0]
How to handle this?: Exception Handling
• To handle exception
properly in Python, We
have four keywords: try,
except, else and finally.
Throw object
at line 3.
Execute PVM
Match the
exception and
Enter first num: 5 make the object of
Enter the second num:0 exception : throw
that in your
program
Line 3 c = a/b
[Error occurred: cannot dived by 0]
• try block :
• if the Python program contains suspicious code that may throw the exception,
we must place that code in the try block.
• The try block must be followed with the except block which contains a block
of code that will be executed if there is some exception in the try block.
• else block –
• It contains the code which should be executed if the exception do not
occur.
• finally block –
• it contains the code which should be executed whether exception
occur or not.
Code:
Possible error:
ZeroDivisionError and ValueError
• Such method handling error is suitable if you want to print the type of error and
message.
• If you want to handle it by writing alternative code then it will have problem.
• Example – For every different questions in exam papers, we didn’t provide
same answer.
• If there is different question, we provide different answers.
Problems:
OR
• We can not write the above both different except block code in a single except block
because both exception do not occurred at same time.
• But point to remember is that both exception can occurred in same code at a single
time.
• So then what will be the solution????? try block with multiple except blocks
try with multiple except blocks:
If exception occurred.
1
2
?
?
Single except block that can handle multiple different exceptions:
• except(exception1,exception2, ….):
• except(exception1, exception2, …..) as msg:
Case1: if no exception occur. Case2: if exception raised Case3: if exception raised and
and handle not handle.
?
finally block:
• Finally block is always executed whether the exception occurred or
not.
• Is finally block is always executed?
• No, there is one situation, if the try block is executing and suddenly power is
off. In this situation the pvm get exit and the whole process will stop.
• os._exit(0) 0 means normal termination. non zero abnormal termination.
else block:
• Else block is executed if no exception occurred at try block.
else block:
• There is no chance of executing both except and else simultaneously.