How to properly ignore exceptions

前端 未结 11 2256
粉色の甜心
粉色の甜心 2020-11-22 02:18

When you just want to do a try-except without handling the exception, how do you do it in Python?

Is the following the right way to do it?

try:
    s         


        
11条回答
  •  不要未来只要你来
    2020-11-22 03:05

    When you just want to do a try catch without handling the exception, how do you do it in Python?

    It depends on what you mean by "handling."

    If you mean to catch it without taking any action, the code you posted will work.

    If you mean that you want to take action on an exception without stopping the exception from going up the stack, then you want something like this:

    try:
        do_something()
    except:
        handle_exception()
        raise  #re-raise the exact same exception that was thrown
    

提交回复
热议问题