First page Back Continue Last page Summary Graphics

Exceptions

  • try:
  • print '5' + 5
  • except TypeError:
  • print "You can't add an integer to a string"
  • try:
  • failure()
  • except:
  • print 'Catch all exceptions this way'
  • try:
  • print [ int(x) for x in (1, '2', 3.3, 'a') ]
  • except (AttributeError, TypeError, ValueError), detail:
  • print 'One of the 3 exceptions was raised:', detail
  • except:
  • print 'All other exceptions would be caught here'

    Notes: