python异常

2018-10-09  本文已影响0人  MagicalGuy

encoding:utf-8

在Python中,所有的标识符都是一个标签

f = print
print('123456')
type(print)
print = 123
f(type(print))
def fun3( function ):
function('123456')
fun3(print)

从异常基类派生出一个自定义异常类

class NetConnectError(Exception):
def init(self , errorMsg=''):
NetConnectError.what= errorMsg

def fun(name):
if not isinstance(name,str):
raise TypeError('类型错误')
print('类型正确')

print(isinstance(123 , str))
try:
fun(123)
except Exception as err:
print('fun产生异常:' , err)

try:
print(a)
l1 = [1,2,3,4]
l1[4] = 0
except Exception as err: # 将IndexError异常对象保存到err中
print('产生异常:',err)

print('ssssss')

----------------

try 和 finally

def fun2():

try:

f = open('123.txt','w')

if( 判断)

return

elif( 判断)

return

elif (判断)

return

finally:

f.close()

fun2()

上一篇下一篇

猜你喜欢

热点阅读