Handle specific exception type in python

后端 未结 7 1671
一向
一向 2021-02-07 02:24

I have some code that handles an exception, and I want to do something specific only if it\'s a specific exception, and only in debug mode. So for example:

try:
         


        
7条回答
  •  面向向阳花
    2021-02-07 03:09

    What's wrong with

    try:
        stuff()
    except KeyboardInterrupt:
        if _debug:
            logging.exception("Debug handling")
            sys.exit()
        else:
            logging.exception("Normal handling")
    

提交回复
热议问题