简体   繁体   中英

how to raise a custom exception in main method proper?

I am trying to understand how to raise a custom error in main method with action. Below is my sudo code but don't work.

I want to raise a custom exception that will go to except in main method with tag, message value. but the below code output a class:

class ' main .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()

It seems that you are printing an object so it will not print your message for that you have to change in your main() .

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

Now you can get your expected output.
You can refer User Defined Exception for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM