日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Python 2.7 Exception格式化工具

發布時間:2025/4/9 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 2.7 Exception格式化工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先說一個發現:

1 try: 2 拋錯誤,拋異常 3 except Exception as e: 4 都被這里抓住了 5 except Error as e: 6 這里啥事都沒了


然后,說說Exception as e的e。e可謂是格式五花八門。

比如:

再比如:

這么不嚴謹的格式,實在沒辦法直接從e.Name或e.Message等之類的主流方式來一步到位地抓出ErrorTitle與ErrorDetail。那怎么辦呢?進行了各種嘗試后,發現個好方法:

str(type(e)) 可以抓出Title,而且還包含Type。而str(e)則可以抓出Detail。

因此,誕生了格式化工具:

ExceptionX_Result.py:

1 class ExceptionX_Result: 2 exceptionType = None 3 exceptionTitle = None 4 exceptionDetail = None;

ExceptionX.py:

1 from ExceptionX_Result import ExceptionX_Result 2 3 class ExceptionX: 4 5 @staticmethod 6 def ToString(arg_exception) : 7 result = ExceptionX_Result 8 tempStr = str(type(arg_exception)) 9 tempStrArray = tempStr.split("'") 10 result.exceptionTitle = tempStrArray[1] 11 result.exceptionType = tempStrArray[0][1:] 12 result.exceptionDetail = str(arg_exception) 13 if result.exceptionDetail[0] == "<": 14 if result.exceptionDetail[result.exceptionDetail.__len__() - 1] == ">" : 15 result.exceptionDetail = result.exceptionDetail[1:result.exceptionDetail.__len__() - 1] 16 return result;

?

用法例子:

1 try: 2 value = 1 / 0 3 except Exception as e: 4 #在下面這行下一個斷點,然后看看tempExceptionInfo的結構吧~ 5 tempExceptionInfo = ExceptionX.ToString(e)

?


呵呵,終于統一了。最終效果:

再如:

轉載于:https://www.cnblogs.com/xxxteam/p/3515590.html

總結

以上是生活随笔為你收集整理的Python 2.7 Exception格式化工具的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。