python学习 异常详解编程语言

#===========================常见异常 ===================== 
#print(a);#NameError: name 'a' is not defined 变量未定义
#if True:#SyntaxError: unexpected EOF while parsing 语法错误
#f=open("123.txt");#FileNotFoundError: [Errno 2] No such file or directory: '123.txt' 没有这个文件 #print(10/0);
# ZeroDivisionError: division by zero 除数为 0错误 #print(int("a"));
# ValueError: invalid literal for int() with base 10: 'a' 值转换无效
#-*-config=utf-8 -*- 
#===========================异常处理 (捕获运行时异常) ===================== 
#常见异常 
#print(a);#NameError: name 'a' is not defined  变量未定义 
#if True:#SyntaxError: unexpected EOF while parsing 语法错误 
#f=open("123.txt");#FileNotFoundError: [Errno 2] No such file or directory: '123.txt' 没有这个文件 
#print(10/0);# ZeroDivisionError: division by zero 除数为 0错误 
#print(int("a"));# ValueError: invalid literal for int() with base 10: 'a' 值转换无效 
#=====================  try - except 语法 ================== 
def tryTest_1(): 
try: 
print(10/0); 
except:# 捕获所有类型异常 
print("Zero   Error");# Zero   Error 
#tryTest_1(); 
def tryTest_2(): 
try: 
print(10/0); 
except ZeroDivisionError as e:#捕获 指定类型的异常 
print("Zero   Error");# Zero   Error 
#tryTest_2(); 
#案例 猜数字游戏 
import random; 
def guessNum(): 
num=random.randint(0,100);#生成一个随机数 
while True: 
try: 
inputNum=int(input()); 
except ValueError as e: 
print("请输入1-100的数字"); 
continue; 
if num>inputNum: 
print("输入的太小"); 
if num<inputNum: 
print("输入的太大"); 
if(num==inputNum): 
print("Ok"); 
break; 
#guessNum(); 
#======================同时处理多个异常==================== 
def tryTest_3(): 
try: 
f=open("2.txt"); 
except FileNotFoundError as  e: 
print("文件不存在");#文件不存在 
except ValueError as e: 
print("错误的值"); 
#tryTest_3(); 
#===================== try - except - else============== 
#如果存在异常代码执行相应的异常类型捕获,如果不存在异常代码执行else 
def tryTest_4(): 
try: 
print(1+1); 
#f=open("2.txt"); 
except FileNotFoundError as  e: 
print("文件不存在");#文件不存在 
except ValueError as e: 
print("错误的值"); 
else: 
print("No Error"); 
#tryTest_4(); 
#===================== try finally  ============ 
#无论是否检查到异常,都会执行finally代码 
#作用:为异常处理事件提供清理机制,用来关闭文件或者释放资源。 
def tryTest_5(): 
try: 
f=open("2.txt"); 
finally: 
print("file close"); 
f.close(); 
#tryTest_5(); 
#========================== try -except -finally ============= 
#1、如果 try语句没有捕获异常,执行try代码后,执行finally语句 
#2、如果 try语句捕获到异常,首先执行except语句后执行finally语句 
def tryTest_6(): 
try: 
f=open("2.txt"); 
except FileNotFoundError as e: 
print("文件不存在"); 
finally: 
print("file close"); 
f.close(); 
#tryTest_6(); 
#========================== try -except - else -finally ============= 
#1、如果 try语句没有捕获异常,执行try代码后执行else语句最后执行finally语句 
#2、如果 try语句捕获到异常,首先执行except语句后执行finally语句 
def tryTest_7(): 
try: 
print(10/1); 
except ZeroDivisionError as e:#捕获 指定类型的异常 
print("Zero   Error");# Zero   Error 
else: 
print("else语句"); 
finally: 
print("finally"); 
#tryTest_7(); 
#==============================with 语句====================== 
# with语句用来替换try-except-finall语句 使代码更加简洁 
def withTEst(): 
try: 
with open("E:/python/w_2.txt") as f: 
print(f.readline()); 
except FileNotFoundError as e: 
print("文件不存在"); 
f.close(); 
#withTEst(); 
#=======================raise 语句 ============================== 
#raise  主动抛出异常 
#类似于java中的throw关键字 
def raiseTest(): 
raise IOError("IO异常"); 
#raiseTest(); 
#======================assert语句================================= 
#assert语句:用于检测表达式是否为真,如果为假,引发AssertionError错误 
#语法:assert expression (判断一个表达式) 
# assert expression1, expression2(判断多个表达式) 
import random; 
def assertTest(n): 
assert n>random.randint(0,10); 
print(n); 
#assertTest(3); 
#如果传入的 n 大于随机生成的数则打印n 
#如果传入的n小于随机生成的数 则引发AssertionError错误

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/16289.html

(0)
上一篇 2021年7月19日 19:09
下一篇 2021年7月19日 19:09

相关推荐

发表回复

登录后才能评论