简体   繁体   中英

Is there an easy way to customize try-except error code outputs in python?

I am trying to custimize my error codes in a script that I am writing and I was wondering if I can adjust the output. Here is the scenario. Running this script:

while True:
    try:
        x = "Hello World"
        int(x)
    except (ValueError, RuntimeError, TypeError, NameError, SystemExit):

        sys.exit("Error: Check Section 1.0")

Creates an output of:

An exception has occurred, use %tb to see the full traceback.
SystemExit: Error: Check Section 1.0

Is there an easy way, when an error occurs, the script stops and only says:

Error: Check Section 1.0

Try this instead, this method direct shows you "Error: Check Section 1.0" only.

while True:
    try:
       x = "Hello World"
       int(x)
    except:
       sys.exit("Error: Check Section 1.0")

Just do print and do break since you keep it inside a loop

while True:
    try:
        x = "Hello World"
        int(x)
    except:
        print("Error: Check Section 1.0")
        break

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