Python

Python Python Python

  • : Python
  • (Assertions):Python

python

BaseException
SystemExit
KeyboardInterrupt (^C)
Exception
StopIteration
GeneratorExit (generator)
StandardError
ArithmeticError
FloatingPointError
OverflowError
ZeroDivisionError () ()
AssertionError
AttributeError
EOFError ,EOF
EnvironmentError
IOError /
OSError
WindowsError
ImportError /
LookupError
IndexError (index)
KeyError
MemoryError (Python )
NameError / ()
UnboundLocalError
ReferenceError (Weak reference)
RuntimeError
NotImplementedError
SyntaxErrorPython
IndentationError
TabError Tab
SystemError
TypeError
ValueError
UnicodeError Unicode
UnicodeDecodeError Unicode
UnicodeEncodeError Unicode
UnicodeTranslateError Unicode
Warning
DeprecationWarning
FutureWarning
OverflowWarning (long)
PendingDeprecationWarning
RuntimeWarning (runtime behavior)
SyntaxWarning
UserWarning

Python

Python

Python


try/except

try/excepttryexcept

try

try....except...else

try:
<>        #
except <>
<>        #try'name'
except <><>:
<>        #'name'
else:
<>        #

trytrypythontry

  • trypythontryexcepttry
  • tryexcepttry
  • trypythonelseelsetry

#!/usr/bin/python
# -*- coding: UTF-8 -*-

try:
    fh = open("testfile", "w")
    fh.write("!!")
except IOError:
    print "Error: "
else:
    print ""
    fh.close()

$ python test.py 

$ cat testfile       # 
!!

#!/usr/bin/python
# -*- coding: UTF-8 -*-

try:
    fh = open("testfile", "w")
    fh.write("!!")
except IOError:
    print "Error: "
else:
    print ""
    fh.close()

testfile

chmod -w testfile

$ python test.py 
Error: 

except

except

try:
    
   ......................
except:
    
   ......................
else:
    

try-except


except

except

try:
    
   ......................
except(Exception1[, Exception2[,...ExceptionN]]):
   
   ......................
else:
    

try-finally

try-finally

try:
<>
finally:
<>    #try
raise

#!/usr/bin/python
# -*- coding: UTF-8 -*-

try:
    fh = open("testfile", "w")
    fh.write("!!")
finally:
    print "Error: "

$ python test.py 
Error: 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

try:
    fh = open("testfile", "w")
    try:
        fh.write("!!")
    finally:
        print ""
        fh.close()
except IOError:
    print "Error: "

tryfinally

finallyexcept


except

try:
    
   ......................
except ExceptionType, Argument:
     Argument ...

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#
def temp_convert(var):
    try:
        return int(var)
    except ValueError, Argument:
        print "\n", Argument

#
temp_convert("xyz")

$ python test.py 

invalid literal for int() with base 10: 'xyz'

raise

raise

raise [Exception [, args [, traceback]]]

Exception NameErrorargs

Python

def functionName( level ):
    if level < 1:
        raise Exception("Invalid level!", level)
        #

"except"

"except"

try:
    
except Exception,err:
        
else:
    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#
def mye( level ):
    if level < 1:
        raise Exception,"Invalid level!"
        #
try:
    mye(0)            #
except Exception,err:
    print 1,err
else:
    print 2

$ python test.py 
1 Invalid level!

Exception

RuntimeError,RuntimeError

tryexcept e Networkerror

class Networkerror(RuntimeError):
    def __init__(self, arg):
        self.args = arg

try:
    raise Networkerror("Bad hostname")
except Networkerror,e:
    print e.args