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

Viewed 38

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
2 Answers

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
Related