sys.exit()函数是通过抛出异常的方式来终止进程的,也就是说如果它抛出来的异常被捕捉到了的话程序就不会退出了。

#!/usr/bin/python
#!coding:utf-8


import sys

if __name__=="__main__":
    try:
        sys.exit(100)
    except SystemExit,err:
        print '捕捉到SystemExit异常,SystemExit 并不派生自Exception 所以用Exception对它没有用'
        print 'SystemExit 异常信息如下: {0}'.format(err)
    print '并没有退出呀...'

 

输出信息如下:

捕捉到SystemExit异常,SystemExit 并不派生自Exception 所以用Exception对它没有用
SystemExit 异常信息如下: 100
并没有退出呀...

 

相关文章:

  • 2022-12-23
  • 2021-08-13
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2021-11-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-12-19
  • 2021-05-27
  • 2021-06-30
  • 2022-12-23
相关资源
相似解决方案