Skip to content
Advertisement

Using sys.exit or SystemExit; when to use which?

Some programmers use sys.exit, others use SystemExit.

  1. What is the difference?
  2. When do I need to use SystemExit or sys.exit inside a function?

Example:

ref = osgeo.ogr.Open(reference)
if ref is None:
    raise SystemExit('Unable to open %s' % reference)

or:

ref = osgeo.ogr.Open(reference)
if ref is None:
    print('Unable to open %s' % reference)
    sys.exit(-1)

Advertisement

Answer

No practical difference, but there’s another difference in your example code – print goes to standard out, but the exception text goes to standard error (which is probably what you want).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement