簡體   English   中英

如何在主要方法中正確引發自定義異常?

[英]how to raise a custom exception in main method proper?

我試圖了解如何通過操作在主要方法中引發自定義錯誤。 下面是我的 sudo 代碼,但不起作用。

我想提出一個自定義異常,它將 go 在帶有標簽、消息值的 main 方法中除外。 但以下代碼 output 為 class:

class'.pdv_error_response'

class my_exceptions(Exception):
   """Base class for other exceptions"""
   pass

class pdv_error_response(my_exceptions):
    def __init__(self, tag, message):
        self.tag = tag
        self.tag = message

def tryerror(x):
    if x < 0:
        raise(pdv_error_response('test','output'))

def main():
    try:
        tryerror(-1)
    except:
        if pdv_error_response:
            print(pdv_error_response)

if __name__ == '__main__':
    main()

您似乎正在打印 object 因此它不會打印您的消息,因為您必須在main()中進行更改。

def main():
    try:
        tryerror(-1)
    except pdv_error_response as e:
        if pdv_error_response:
            print(e)

現在您可以獲得您所期望的 output。
您可以參考用戶定義的異常了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM