python catch exception and continue try block

前端 未结 9 2385
眼角桃花
眼角桃花 2020-11-29 00:59

Can I return to executing try-block after exception occurs? (The goal is to write less) For Example:

try:
    do_smth1()
except:
    pass

try:
    do_smth2(         


        
相关标签:
9条回答
  • 2020-11-29 01:03

    Depending on where and how often you need to do this, you could also write a function that does it for you:

    def live_dangerously(fn, *args, **kw):
        try:
            return fn(*args, **kw)
        except Exception:
            pass
    
    live_dangerously(do_smth1)
    live_dangerously(do_smth2)
    

    But as other answers have noted, having a null except is generally a sign something else is wrong with your code.

    0 讨论(0)
  • 2020-11-29 01:10

    You could iterate through your methods...

    for m in [do_smth1, do_smth2]:
        try:
            m()
        except:
            pass
    
    0 讨论(0)
  • 2020-11-29 01:16

    'continue' is allowed within an 'except' or 'finally' only if the try block is in a loop. 'continue' will cause the next iteration of the loop to start.

    So you can try put your two or more functions in a list and use loop to call your function.

    Like this:

    funcs = [f,g]
    for func in funcs:
        try: func()
        except: continue
    

    For full information you can go to this link

    0 讨论(0)
  • 2020-11-29 01:17

    No, you cannot do that. That's just the way Python has its syntax. Once you exit a try-block because of an exception, there is no way back in.

    What about a for-loop though?

    funcs = do_smth1, do_smth2
    
    for func in funcs:
        try:
            func()
        except Exception:
            pass  # or you could use 'continue'
    

    Note however that it is considered a bad practice to have a bare except. You should catch for a specific exception instead. I captured for Exception because that's as good as I can do without knowing what exceptions the methods might throw.

    0 讨论(0)
  • 2020-11-29 01:17

    While the other answers and the accepted one are correct and should be followed in real code, just for completeness and humor, you can try the fuckitpy ( https://github.com/ajalt/fuckitpy ) module.

    Your code can be changed to the following:

    @fuckitpy
    def myfunc():
        do_smth1()
        do_smth2()
    

    Then calling myfunc() would call do_smth2() even if there is an exception in do_smth1())

    Note: Please do not try it in any real code, it is blasphemy

    0 讨论(0)
  • 2020-11-29 01:20

    one way you could handle this is with a generator. Instead of calling the function, yield it; then whatever is consuming the generator can send the result of calling it back into the generator, or a sentinel if the generator failed: The trampoline that accomplishes the above might look like so:

    def consume_exceptions(gen):
        action = next(gen)
        while True:
            try:
                result = action()
            except Exception:
                # if the action fails, send a sentinel
                result = None
    
            try:
                action = gen.send(result)
            except StopIteration:
                # if the generator is all used up, result is the return value.
                return result
    

    a generator that would be compatible with this would look like this:

    def do_smth1():
        1 / 0
    
    def do_smth2():
        print "YAY"
    
    def do_many_things():
        a = yield do_smth1
        b = yield do_smth2
        yield "Done"
    
    >>> consume_exceptions(do_many_things())
    YAY
    

    Note that do_many_things() does not call do_smth*, it just yields them, and consume_exceptions calls them on its behalf

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