Catching Exceptions : Try : Statement PYTHON TUTORIALS


PYTHON TUTORIALS » Statement » Try »

 

Catching Exceptions


try
    x = input('Enter the first number: ')
    y = input('Enter the second number: ')
    print x/y
except ZeroDivisionError: 
    print "The second number can't be zero!"

class MyCalc:
   muffled = 0
   def calc(self, expr):
      try:
        return eval(expr)
      except ZeroDivisionError:
       if self.muffled:
          print 'Division by zero is illegal'
      else:
          raise 

calculator = MyCalc()
calculator.calc('10/2')
calculator.calc('10/0') # No muffling

calculator.muffled = 
calculator.calc('10/0')



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Statement
» Try