Python 板


LINE

各位版友好, 最近在运用exception时遇到一个流程设计的问题, 假设我有3个函式,分别是a、b、c, 其中a会呼叫b和c,b会呼叫c 若是想做到只要有函式出现例外,整个程式就停止运作, 是否只能用判断式一一检查执行完的函式是否正常跑完呢? 还是有其他种作法呢? 如果只能用判断式一一检查被呼叫的函式执行结果,若a大量呼叫b和c,不就会充斥很多 判断式在a中吗? 附上范例... #main.py from test import test if __name__=='__main__': t=test() t.test() ------------------------------------------------------------ #test.py from ErrManager import ErrManager from test2 import test2 class test: def test(self): try: print("test:test2 begin") t2=test2() t2.test2() print("test:test2 end") except ErrManager as e: print(e) ------------------------------------------------------------ #test2.py from ErrManager import ErrManager from test3 import test3 class test2: def test2(self): try: print("test2:test3 begin") t3=test3() t3.test3() print("tes2:test3 end") raise ErrManager("Exception in test2") except ErrManager as e: print(e) ------------------------------------------------------------- #test3.py from ErrManager import ErrManager class test3: def test3(self): try: raise ErrManager("Exception in test3") except ErrManager as e: print(e) ------------------------------------------------------------- #ErrManager class ErrManager(Exception): def __init__(self,arg): self.arg=arg ------------------------------------------------------------- 我以为运作结果会印出 test:test2 begin test2:test3 begin Exception in test3 但实际上印出 test:test2 begin test2:test3 begin Exception in test3 tes2:test3 end Exception in test2 test:test2 end 请问是我运用Exception的方式有错吗? --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.137.25.175 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1575624119.A.9D8.html ※ 编辑: Dong0129 (114.137.25.175 台湾), 12/06/2019 17:22:39
1F:推 sherees: 你可以把try except写在b, c里面12/06 17:37
有的,设计上每个function都有自己的try-except, 但被呼叫的函式执行发生exception後,在呼叫的地方还是要检查一次才会停止运作... ※ 编辑: Dong0129 (114.137.25.175 台湾), 12/06/2019 17:49:30
2F:推 benson415: 如果要中断,何不raise error? 12/06 19:43
有的,我是使用自定义的例外,文末附上范例代码...若我对於Exception运用理解错误还 请提出指教。 ※ 编辑: Dong0129 (220.137.94.193 台湾), 12/06/2019 22:19:21 ※ 编辑: Dong0129 (220.137.94.193 台湾), 12/06/2019 22:19:54 ※ 编辑: Dong0129 (220.137.94.193 台湾), 12/06/2019 22:23:17
3F:→ stucode: 因为你 except 把例外拦下来了,想让它向外传播的话 12/06 23:13
4F:→ stucode: 在最外层或你想处理例外的地方 try except 就好 12/06 23:13
5F:→ stucode: 如果想在中间做些 logging 可以先拦下来再 re-raise 12/06 23:13
6F:→ benson415: 按照你的写法,印出来的确实是会这样没错。如果真的 12/07 22:03
7F:→ benson415: 想中断又不大改的话就是直接在test3里面把except里的p 12/07 22:03
8F:→ benson415: rint(e)改成raise e 12/07 22:03
简单来说应该是我try-except写太多层吧?只要在最上层写一个try-except就好了...感谢 各位的指导 ※ 编辑: Dong0129 (118.167.123.168 台湾), 12/08/2019 21:44:55







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP