1

In my code I'm trying to handle a logical error. I want to quit the program with an error code, if I do a sys.exit(1) it raises a traceback error, which I don't want. But, if I do os._exit(1) it not the cleaner exit.

My need: Quit like with os with just the error code, but need a cleaner exit like with sys.

Ulf Gjerdingen
  • 1,414
  • 3
  • 16
  • 20

1 Answers1

2

Use sys.exit(1) anyway. The standard Python traceback hook in sys.excepthook() handles SystemExit cleanly, no traceback is printed. From the SystemExit exception documentation:

This exception is raised by the sys.exit() function. When it is not handled, the Python interpreter exits; no stack traceback is printed.

Bold emphasis mine.

If your program has a top-level exception handler, don't catch SystemExit. Usually you do that by not using a blanket except: statement, but by catching except Exception: instead.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343