#example program on without Exception
Handling
print("Good
afternoon")#
print(4/2)#
print(10/0)#exception raised
print("Thank you")
output:
Good afternoon
2.0
Runtime error : Abnormal
termination
with using try..except
#example program on Exception
Handling
print("Good
afternoon")#
print(4/2)#
try:
print(10/0)#exception raised
except ZeroDivisionError:
print("division by zero is not
possible")
print("Thank you")
Ouput:
Good afternoon
2.0
Division by zero is not
possible
Thank you
Exception:
An exception is on error,
which can occur during runtime and which can be handled by the programmer. That
means if programmer can guess an error in the program and he can avoid the
abnormal termination by using try..except keywords.
Types of exceptions
1.
Predefined
exception (built in exceptions)
2.
User defined
exceptions
Predefined exception are
represented as classes.
Purpose Exception Handling:
To make the program more
powerful (robust=strong). If we are handling the exceptions, our
program/application will not terminate abnormally during execution.
Steps to handling the
exceptions:
1.
As a programmer
should observe the code where there may be possible of exceptions, such
exceptions should be kept in side the try block.
Example:
try:
10/0 #risky
code
2. We should write the corresponding exception class name
with except keyword. This step is called handling the exception
Ex:
except for DivisionByZero:
Any message
3.
finally, this keyword is used to clean up the
code example closing the files.
Whenever we write the
statements in side the finally block, those statements will be executed
irrespective of the exceptions.
The above 3 steps are called
the exception handling.
Syntax:
try:
Risky code /Run this code
except NameOfexception:
run this code if exceptions occur in the try block
#Example program
def concat(a,b):
print(a+b)
try:
concat("hyd",25)
print("good morning")
except TypeError:
print("Only strings can be
concatinated but not one string, one number")
"""output:
Case1
Only strings can be
concatenated but not one string, one number
>>>
Case2: If you want
to execute the statement irrespective of exception, use finally block
def concat(a,b):
print(a+b)
try:
concat("hyd",25)#exception raised
except TypeError:
print("Only strings can
be concatinated but not one string, one number")
finally:
print("good
morning")
"""output:
Only strings can be
concatenated but not one string, one number
good morning
Case3: exception can be
more than one but try should be only one
def concat(a,b):
print(a+b)
try:
concat("hyd",25)#exception raised
print(10/0)
except TypeError:
print("Only strings can be
concatinated but not one string, one number")
except ZeroDivisionError:
print("Division by zero is not
possible")
finally:
print("good morning")
"""output:
Case2
Only strings can be
concatenated but not one string, one number
good
morningRelated video: https://youtu.be/oC6v7WLzZFw
Next: https://youtu.be/dq9W6ig91QQ
Prev:Exceptions:
https://youtu.be/TeVaBIo-WQo
No comments:
Post a Comment