How to handle python exception inside if statement?


The code can be written as follows to catch the exception

a, b=5, 0
try:
   if b != 0:
       print a/b
   else:
       a/b
       raise ZeroDivisionError
except Exception as e:
       print e

We get the following output

C:/Users/TutorialsPoint1/~.py
integer division or modulo by zero

Updated on: 27-Sep-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements