简体   繁体   中英

How do i return error in exception in python?

I want to return the error in my code that I wrote in python. I can't do this. How can I do it?

def proc():
    try:
        a=2/0
    except Exception as e:
        print("Except")
        raise f"{e}"
    else:
        return "Success"

result=proc()
print("result : ",result)

I tried using direct raise but it didn't work? How can I do?

If you just want to return the error message with the class name, you could probably do this:

def proc():
    try:
        a=2/0
    except Exception as e:
        print("Except")
        return repr(e) # Repr is a great solution
    else:
        return "Success"

result=proc()
print("result : ",result)

Result:

Except
result :  ZeroDivisionError(division by zero)

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