Mega Code Archive

 
Categories / Python Tutorial / Statement
 

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 = 1  calculator.calc('10/0')