Execute (part of) try block after except block?

A try... except block catches one exception. That's what it's for. It executes the code inside the try, and if an exception is raised, handles it in the except.

You can't raise multiple exceptions inside the try.

A try... except... block catches one exception. That's what it's for. It executes the code inside the try, and if an exception is raised, handles it in the except.

You can't raise multiple exceptions inside the try. This is deliberate: the point of the construction is that you need explicitly to handle the exceptions that occur. Returning to the end of the try violates this, because then the except statement handles more than one thing.

You should do: try: do.this() except FailError: clean.up() try: do.that() except FailError: clean.up() so that any exception you raise is handled explicitly.

... try: do.this() except: foo.bar() ... finally: do.that() ...

I wasn't clear in my question. Actually the exception can be raised from this() or that() or wherever, it doesn't care, but the the point is to "return" in some way to the try block. "Return" or execute in some manner the code in the try block after the exception – Niko P Nov 19 '10 at 14:48.

I don't need to execute always foo.bar(). That was just an example. What I'm trying to do is to return to the try: block after the execution of an except: block – Niko P Nov 19 '10 at 14:32 1 @Niko P, you cannot return to the try block if an exception is raised.

But you can restructure your code so that you don't need to. – Klaus Byskov Hoffmann Nov 19 '10 at 14:34 I know that I cannot return to the try block. But supposing that I am able to fix do.this() (or any other operation within that try block) at runtime, how can I resume the execution?

Only splitting every single call in a try/execute block? – Niko P Nov 19 '10 at 14:43.

You need two try blocks, one for each statement in your current try block.

One possibility is to write a code in such a way that you can re-execute it all when the error condition has been solved, e.g. : while 1: try: complex_operation() except X: solve_problem() continue break.

This way re-run all the try: block. I would like to re-start the execution from the next instruction of the failing one. – Niko P Nov 19 '10 at 14:36 That's why I said "write the code in such a way that you can re-execute it all", e.g. Code that doesn't need to reacquire resources, things like that.

Well, was just a thought. – UncleZeiv Nov 19 '10 at 18:51.

Fcts = do. This, do. That for fct in fcts: try: fct() except: foo.bar().

This doesn't scale up well, but for smaller blocks of code you could use a classic finite-state-machine: states = do. This, do. That state = 0 while state.

Here's another alternative. Handle the error condition with a callback, so that after fixing the problem you can continue. The callback would basically contain exactly the same code you would put in the except block.As a silly example, let's say that the exception you want to handle is a missing file, and that you have a way to deal with that problem (a default file or whatever).

FileRetriever is the callback that knows how to deal with the problem. Then you would write: def myOp(fileRetriever): f = acquireFile() if not f: f = fileRetriever() # continue with your stuff... f2 = acquireAnotherFile() if not f2: f2 = fileRetriever() # more stuff... myOp(magicalCallback) Note: I've never seen this design used in practice, but in specific situations I guess it might be usable.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions