Catch more than on exceptions : except « Statement « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » Statement » except 




import sys, traceback, math

try:
    n = float(raw_input("Please, enter a number: "))
    print "The sqrt of %f is %f" (n, math.sqrt(n))

except (ValueError, TypeError, OverflowError):
    print "This is the standard traceback message:"
    print ""
    traceback.print_exc()
    print "This is the customized traceback message:"
    print ""
    info = sys.exc_info()
    exc_type = info[0]
    exc_value = info[1]
    exc_traceback = info[2]

    trace = traceback.extract_tb(sys.exc_traceback)
    print "Exception Type:  ", exc_type
    print "Error Message:   ", exc_value
    print "File name:       ", trace[0][0]
    print "Error message:   ", trace[0][1]
    print "Line:            ", trace[0][2]
    print "Function:        ", trace[0][3]
else:
    print "Everything went just fine."














3.10.except
3.10.1.Catching Two Exceptions
3.10.2.Simple exception handling example.
3.10.3.Demonstrating exception arguments and stack unwinding.
3.10.4.Catch more than on exceptions
3.10.5.Catch KeyError and AssertionError
3.10.6.try/except block that checks for correct user input
3.10.7.try/except block with string argument
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.