Professional Documents
Culture Documents
Fs20co047 Py Exp 5
Fs20co047 Py Exp 5
FS20CO047
Experiment 5
SHRIKRUSHNA PATIL-FS20CO047 1
2 Python NO-5
Theory:
Exception Handling:-
Syntax:- try:
#block of code
except Exception1:
#block of code
except Exception2:
#block of code
#other code
SHRIKRUSHNA PATIL-FS20CO047 2
3 Python NO-5
We can also use the else statement with the try-except statement in
which, we can place the code which will be executed in the scenario
if no exception occurs in the try block.
Syntax:- try:
#block of code
except Exception1:
#block of code
else Exception2:
#this code executes if no except block is excuted
SHRIKRUSHNA PATIL-FS20CO047 3
4 Python NO-5
Syntax:- try:
#block of code
except (<Exception1>,<Exception2>,<Exception3>,…<Exception
n>)
#block of code
else Exception2:
#this code executes if no except block is excuted
Syntax:-
try:
# block of code
# this may throw an exception
finally:
# block of code
# this will always be executed
4. Raising exceptions:-
An exception can be raised forcefully by using the raise clause
in Python. It is useful in in that scenario where we need to raise an
exception to stop the execution of the program.
For example, there is a program that requires 2GB memory for
execution, and if the program tries to occupy 2GB of memory,
SHRIKRUSHNA PATIL-FS20CO047 4
5 Python NO-5
Syntax:-
raise Exception_class,<value>
Practical:
a. Check Divide by zero exception.
Program:-
Observation:
a.
Output:-
SHRIKRUSHNA PATIL-FS20CO047 5
6 Python NO-5
Conclusion:
In the above experiment I learned how to use the Exception Handling and
perform various Program’s using it.
SHRIKRUSHNA PATIL-FS20CO047 6