13

I know using below code to ignore a certain exception, but how to let the code go back to where it got exception and keep executing? Say if the exception 'Exception' raises in do_something1, how to make the code ignore it and keep finishing do_something1 and process do_something2? My code just go to finally block after process pass in except block. Please advise, thanks.

try:
    do_something1
    do_something2
    do_something3
    do_something4
except Exception:
    pass
finally:
    clean_up

EDIT: Thanks for the reply. Now I know what's the correct way to do it. But here's another question, can I just ignore a specific exception (say if I know the error number). Is below code possible?

try:
    do_something1
except Exception.strerror == 10001:
    pass

try:
    do_something2
except Exception.strerror == 10002:
    pass
finally:
    clean_up

do_something3
do_something4
5
  • yes you can do that but not the way you have it written. I've updated my answer. Sep 28, 2010 at 23:45
  • this sounds like VB's on error resume next
    – Lie Ryan
    Sep 28, 2010 at 23:50
  • @Lie Ryan. Is VB still considered a living language? Sep 28, 2010 at 23:52
  • @AaronMcSmooth: try poking at that dead body over there
    – Lie Ryan
    Sep 28, 2010 at 23:55
  • 2
    "Is below code possible". First try it. Then ask questions if it didn't work.
    – S.Lott
    Sep 29, 2010 at 2:48

7 Answers 7

10

There's no direct way for the code to go back inside the try-except block. If, however, you're looking at trying to execute these different independant actions and keep executing when one fails (without copy/pasting the try/except block), you're going to have to write something like this:

actions = (
    do_something1, do_something2, #...
    )
for action in actions:
    try:
        action()
    except Exception, error:
        pass
9

update. The way to ignore specific exceptions is to catch the type of exception that you want, test it to see if you want to ignore it and re-raise it if you dont.

try:
    do_something1
except TheExceptionTypeThatICanHandleError, e:
    if e.strerror != 10001:
        raise
finally:
     clean_up

Note also, that each try statement needs its own finally clause if you want it to have one. It wont 'attach itself' to the previous try statement. A raise statement with nothing else is the correct way to re-raise the last exception. Don't let anybody tell you otherwise.


What you want are continuations which python doesn't natively provide. Beyond that, the answer to your question depends on exactly what you want to do. If you want do_something1 to continue regardless of exceptions, then it would have to catch the exceptions and ignore them itself.

if you just want do_something2 to happen regardless of if do_something1 completes, you need a separate try statement for each one.

try:
   do_something1()
except:
   pass

try:
   do_something2()
except:
   pass

etc. If you can provide a more detailed example of what it is that you want to do, then there is a good chance that myself or someone smarter than myself can either help you or (more likely) talk you out of it and suggest a more reasonable alternative.

2
  • Yes, the logic being that, Exception s are supposed to signal conditions where normal flow cannot continue: your program needs diverting. Dually, try blocks are supposed to delimit code that is is 'serially dependent' on what went before. Sep 28, 2010 at 23:34
  • Thank you so much @AaronMcSmooth and all other folks!
    – Stan
    Sep 28, 2010 at 23:47
9

This is pretty much missing the point of exceptions.

If the first statement has thrown an exception, the system is in an indeterminate state and you have to treat the following statement as unsafe to run.

If you know which statements might fail, and how they might fail, then you can use exception handling to specifically clean up the problems which might occur with a particular block of statements before moving on to the next section.

So, the only real answer is to handle exceptions around each set of statements that you want to treat as atomic

3

you could have all of the do_something's in a list, and iterate through them like this, so it's no so wordy. You can use lambda functions instead if you require arguments for the working functions

work = [lambda: dosomething1(args), dosomething2, lambda: dosomething3(*kw, **kwargs)]

for each in work:
    try:
        each()
    except:
       pass

cleanup()
1
  • I realize just as I posted this that there was a similar comment, lol. sorry.
    – Blazer
    Sep 28, 2010 at 23:36
2

Exceptions are usually raised when a performing task can not be completed in a manner intended by the code due to certain reasons. This is usually raised as exceptions. Exceptions should be handled and not ignored. The whole idea of exception is that the program can not continue in the normal execution flow without abnormal results.

What if you write a code to open a file and read it? What if this file does not exist?

It is much better to raise exception. You can not read a file where none exists. What you can do is handle the exception, let the user know that no such file exists. What advantage would be obtained for continuing to read the file when a file could not be opened at all.

In fact the above answers provided by Aaron works on the principle of handling your exceptions.

1

I posted this recently as an answer to another question. Here you have a function that returns a function that ignores ("traps") specified exceptions when calling any function. Then you invoke the desired function indirectly through the "trap."

def maketrap(*exceptions):
    def trap(func, *args, **kwargs):
        try:
            return func(*args, **kwargs)
        except exceptions:
            return None
    return trap

# create a trap that ignores all exceptions
trapall = maketrap(Exception) 

# create a trap that ignores two exceptions
trapkeyattrerr = maketrap(KeyError, AttributeError)

# Now call some functions, ignoring specific exceptions
trapall(dosomething1, arg1, arg2)
trapkeyattrerr(dosomething2, arg1, arg2, arg3)

In general I'm with those who say that ignoring exceptions is a bad idea, but if you do it, you should be as specific as possible as to which exceptions you think your code can tolerate.

0

Python 3.4 added contextlib.suppress(), a context manager that takes a list of exceptions and suppresses them within the context:

with contextlib.suppress(IOError):
  print('inside')
  print(pathlib.Path('myfile').read_text()) # Boom
  print('inside end')

print('outside')

Note that, just as with regular try/except, an exception within the context causes the rest of the context to be skipped. So, if an exception happens in the line commented with Boom, the output will be:

inside
outside

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.