There is a slight, subtle difference:
import sys try: sys.exit() except: print("Caught")
that the except
operator catches the exception, whereas:
import system
try: sys.exit() except Exception: print("Caught")
comes out without errors. A SystemExit
exception (e.g. KeyboardInterrupt
) is not a SystemExit
, except Exception
, but except
is executed alone.
Therefore, if the caller catches everything except:
(which is bad practice), your sys.exit
will not exit, but will be considered an “error” (therefore, except Exception:
it’s better to make sure that it catches all except CTRL + C and exit systems (which belong to the BaseException
class).
source share