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 | |
| SyntaxError | Python |
| 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()
# -*- 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()
# -*- 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: "
# -*- 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: "
# -*- 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")
# -*- 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)
#
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
# -*- 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
ljm
131***[email protected]
0
#!/usr/bin/python # -*- coding: UTF-8 -*- try: 1 / 0 except Exception as e: '''''' print "0" else: '''''' print "" finally: print ""ljm
131***[email protected]
Tom
jie***[email protected]
#!/usr/bin/python # -*- coding: UTF-8 -*- #This is note foe exception try code #python except: # #except ExceptionNameargs # code # else #else code #try finally code #try # def try_exception(num): try: return int(num) except ValueError,arg: print arg,"is not a number" else: print "this is a number inputs" try_exception('xxx') # Invalide literal for int() with base 10: 'xxx' is not a numberTom
jie***[email protected]
Alonelk
974***[email protected]
try try-except-else try else unreachable
def try_exception(num): try: return int(num) except ValueError,arg: print arg,"is not a number" else: print "this is a number inputs"
Alonelk
974***[email protected]