Setting exit code in Python when an exception is raised

前端 未结 1 1660
一生所求
一生所求 2020-12-15 15:46
$ cat e.py
raise Exception
$ python e.py
Traceback (most recent call last):
  File \"e.py\", line 1, in 
    raise Exception
Exception
$ echo $?
1


        
相关标签:
1条回答
  • 2020-12-15 16:21

    Take a look at the traceback module. You could do the following:

    import sys, traceback
    
    try:
      raise Exception()
    except:
      traceback.print_exc()
      sys.exit(3)
    

    This will write traceback to standard error and exit with code 3.

    0 讨论(0)
提交回复
热议问题