日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

20個改善編碼的Python異常處理技巧,讓你的代碼更高效

來源: 責編: 時間:2024-02-29 14:44:03 185觀看
導讀異常處理是寫好代碼的一個重要的方面,雖然許多開發人員都熟悉基本的try-except塊,但是有很多更深入的知識可以使異常處理更高效、更可讀和更python化。所以本文將介紹關于Python異常的20個可以顯著改善編碼的Python異常

異常處理是寫好代碼的一個重要的方面,雖然許多開發人員都熟悉基本的try-except塊,但是有很多更深入的知識可以使異常處理更高效、更可讀和更python化。所以本文將介紹關于Python異常的20個可以顯著改善編碼的Python異常處理技巧,這些技巧可以讓你熟練的掌握Python的異常處理。bXk28資訊網——每日最新資訊28at.com

bXk28資訊網——每日最新資訊28at.com

Python中的異常是在程序執行期間發生的破壞了程序指令的正常流程的事件。與其他編程語言一樣,Python使用異常來代表錯誤發生的信號,程序可以做出反應,并恢復或通知用戶產生的問題。bXk28資訊網——每日最新資訊28at.com

1、最簡單的異常處理

我們都知道最簡單的異常處理如下:bXk28資訊網——每日最新資訊28at.com

try:    # Your code here except IOError:    # Handle I/O errors except Exception as e:    # Handle other exceptions finally:    # Cleanup, runs no matter what

異常是按層次結構組織的,如果發生了IOError會執行IOError的except代碼,剩下的異常則交由Exception處理。理解這個層次結構可以根據需要更廣泛或更具體地捕獲錯誤。bXk28資訊網——每日最新資訊28at.com

bXk28資訊網——每日最新資訊28at.com

使用finally子句確保執行清理操作,而不管是否發生異常。它非常適合關閉文件或釋放資源。bXk28資訊網——每日最新資訊28at.com

2、自定義異常

創建自定義異常可以使代碼更具可讀性和可維護性,可以清楚地表示特定的錯誤條件。bXk28資訊網——每日最新資訊28at.com

class MyCustomError(Exception):    pass  try:    raise MyCustomError("A specific error occurred") except MyCustomError as e:    print(e)

3、Else in Try-Except

如果沒有引發異常,則try-except塊中的else子句將運行。這是其他語言沒有的bXk28資訊網——每日最新資訊28at.com

try:    # Attempt operation except Exception:    # Handle error else:    # Executes if no exceptions

4、AS關鍵字

在捕獲異常時,可以使用as關鍵字將異常分配給一個變量,這樣可以顯示詳細信息并使調試更容易。bXk28資訊網——每日最新資訊28at.com

try:    # Some operation except Exception as e:    print(f"Error: {e}")

5、捕獲多個異常

元組可用于在一行中捕獲多種異常類型,從而簡化錯誤處理代碼。bXk28資訊網——每日最新資訊28at.com

try:    # Risky operation except (TypeError, ValueError) as e:    # Handle both exceptions

6、異常觸發另外的異常

Python允許在使用from保持原始回溯的同時觸發新的異常,從而幫助調試復雜的場景。bXk28資訊網——每日最新資訊28at.com

try:    # Some operation except Exception as original_error:    raise RuntimeError("Something bad happened") from original_error

這種方法有好有壞,所以如果不熟悉的話建議還是不要用。bXk28資訊網——每日最新資訊28at.com

7、忽略異常

使用contextlib.suppress()函數,可以優雅地忽略特定的異常,從而使代碼更清晰、更易讀。bXk28資訊網——每日最新資訊28at.com

from contextlib import suppress  with suppress(FileNotFoundError):    # Operation that might not find a file

8、使用斷言

如果不滿足條件,可以使用斷言拋出異常。但是要謹慎使用它們,因為它們可以通過執行時的優化標志被禁用。bXk28資訊網——每日最新資訊28at.com

assert condition, "Condition was not met"

assert 斷言會拋出AssertionError,可以在except中直接捕獲bXk28資訊網——每日最新資訊28at.com

9、格式化異常信息

利用Traceback模塊打印詳細的異常信息,這樣可以顯示完整的錯誤來幫助調試。bXk28資訊網——每日最新資訊28at.com

import traceback  try:    raise ValueError("An error occurred") except:    traceback.print_exc() # Print exception information to stderr

10、使用warnings模塊發出非致命警報

warnings模塊發出是警告而不是異常。如果希望在不停止程序執行的情況下提醒用戶或開發人員潛在問題時,它非常有用。bXk28資訊網——每日最新資訊28at.com

import warnings  warnings.warn("This is a warning message", UserWarning)

11、忽略異常

suppress函數被用來忽略特定的異常。contextlib可以確保資源在使用后得到適當的清理。bXk28資訊網——每日最新資訊28at.com

from contextlManaging Resources: Illustrates creating context managers for resource management, ensuring resources are properly cleaned up after use. The suppress function is shown to ignore specific exceptions.ib import contextmanager, suppress  @contextmanager def managed_resource():    try:        resource = "Resource"        yield resource    finally:        print("Resource cleanup")  with managed_resource() as res:    print(res)  with suppress(FileNotFoundError):    open('non_existent_file.txt', 'r') # Suppresses the FileNotFoundError

12、創建處理異常的包裝器函數

functools模塊可以創建一個裝飾器來包裝用于集中異常處理的函數,從而簡化跨多個函數的錯誤管理。bXk28資訊網——每日最新資訊28at.com

from functools import wraps  def exception_handler(func):    @wraps(func)    def wrapper(*args, **kwargs):        try:            return func(*args, **kwargs)        except Exception as e:            print(f"Handled exception: {e}")            return None    return wrapper  @exception_handler def risky_function():    raise ValueError("Something went wrong")  risky_function()

13、訪問異常相關的屬性和函數

使用sys.exc_info()可以獲取有關當前異常的詳細信息,這對于進一步記錄或處理錯誤細節很有用。bXk28資訊網——每日最新資訊28at.com

import sys  try:    raise TypeError("An error occurred") except:    exc_type, exc_value, exc_traceback = sys.exc_info()    print(exc_type, exc_value)

14、分析當前異常上下文

利用inspect模塊分析當前異常上下文,這對于復雜的錯誤處理場景特別有用。bXk28資訊網——每日最新資訊28at.com

import inspect  def current_exception():    for frame in inspect.trace():        if frame[3] == 'risky_function':            return frame[0].f_locals.get('e')  try:    risky_function() except Exception as e:    print(current_exception())

15、創建動態異常類

types模塊可以動態創建異常類。這對于基于運行時條件動態生成自定義異常非常有用。bXk28資訊網——每日最新資訊28at.com

import types  DynamicException = types.new_class('DynamicException', (Exception,)) raise DynamicException("A dynamically created exception")

16、訪問所有內置異常

builtins可以列出Python中可用的所有內置異常,幫助我們了解層次結構和各種異常。bXk28資訊網——每日最新資訊28at.com

import builtins  for name in dir(builtins):    obj = getattr(builtins, name)    if isinstance(obj, type) and issubclass(obj, BaseException):        print(name)

17、自定義異常的字符串表示形式

可以通過覆蓋__str__和__repr__方法來演示自定義異常,獲得更多信息豐富的錯誤消息。bXk28資訊網——每日最新資訊28at.com

class MyException(Exception):    def __str__(self):        return "This is a custom message for MyException"     def __repr__(self):        return "MyException()"  raise MyException

18、創建不被except Exception捕獲的異常

常規except的Exception塊會捕獲從BaseException派生的異常,比如非常嚴重的錯誤我們可以派生字BaseException。bXk28資訊網——每日最新資訊28at.com

class MyCriticalError(BaseException):    pass  try:    raise MyCriticalError("A critical error") except Exception as e:    print("This will not catch MyCriticalError")

19、優雅的處理用戶和系統中斷

捕獲KeyboardInterrupt和SystemExit異常,以優雅地處理用戶或系統啟動的關機。bXk28資訊網——每日最新資訊28at.com

import sys  try:    while True:        continue except KeyboardInterrupt:    print("User interrupted the process")    sys.exit(0)

20、生成器的資源回收

GeneratorExit表示生成器執行時產生了異常,捕獲它可以在關閉生成器時進行清理操作。bXk28資訊網——每日最新資訊28at.com

def my_generator():    try:        yield "Hello"    except GeneratorExit:        print("Generator closing")        raise  gen = my_generator() next(gen) gen.close()

總結

Python異常可以極大地增強代碼的健壯性和清晰度。本文整理的20個異常處理代碼示例可以幫助你充分利用Python的錯誤處理能力,顯著改善代碼的異常處理能力。bXk28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-75370-0.html20個改善編碼的Python異常處理技巧,讓你的代碼更高效

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 探索C++之美:封裝、繼承、多態的奇妙世界

下一篇: 53 道 JavaScript 前端基礎面試題

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 江安县| 盐山县| 古浪县| 高青县| 高要市| 交口县| 辰溪县| 阜南县| 苗栗市| 贵港市| 伊宁市| 安陆市| 天长市| 达孜县| 五台县| 江永县| 神农架林区| 兴文县| 饶平县| 高雄县| 丽江市| 保康县| 南郑县| 黑龙江省| 溧阳市| 长顺县| 华池县| 铜川市| 吉安市| 万安县| 五家渠市| 平远县| 卢龙县| 皋兰县| 荣成市| 交口县| 江阴市| 嘉峪关市| 海阳市| 商洛市| 运城市|