2

I know how to catch exceptions and print the message they returned:

class SelfDefinedException(Exception): pass

try:
    message = "Hello World!"
    raise SelfDefinedException(message)
except MyDefinedException, e:
    print "MyDefinedException", e

This works good so far.

But how can I catch and print the message in a 'finally' clause?

class SelfDefinedException(Exception): pass

try:
    message = "Hello World!"
    raise SelfDefinedException(message)
except MyDefinedException, e:
    print "MyDefinedException", e
finally:
    # What goes here? So I can see what went wrong?

From several answers I understand, that this is not possible. Is it ok to do something like this?

class SelfDefinedException(Exception): pass

try:
    message = "Hello World!"
    raise SelfDefinedException(message)
except MyDefinedException, e:
    print "MyDefinedException", e
except Exception, e:
    # Hopefully catches all messages except for the one of MyDefinedException
    print "Unexpected Exception raised:", e
3
  • Why do you want to? Isn't the except clause good enough? Commented Jun 21, 2011 at 22:45
  • @Winston, I implemented a handling of my own Exceptions I raise, but I want to be informed if somehow another exception was raised, that I did not expect. See my edit. Commented Jun 21, 2011 at 22:50
  • 1
    yes, if you want to catch all other exceptions, just keep listing them with more except clauses. see documentation Commented Jun 21, 2011 at 22:57

4 Answers 4

4

the code in the finally block will always be evaluated. check to see what went wrong in the catch block

Sign up to request clarification or add additional context in comments.

2 Comments

I understand, that finally catches any Exception except for SelfDefinedException in my example. But how do I get to visualize in the finally` block the Message and the type of exception raised in the try block?
@Druss, The finally clause doesn't catch any exceptions. It is always executed even in the successful case
4

According to the documentation, you can't:

The exception information is not available to the program during execution of the finally clause.

Best to check in the except block.

Comments

2

To catch anything at all use:

try:
    foo()
except:
    print sys.exc_info()
    raise

But this is almost always the wrong thing to do. If you don't what kind of exception happened there isn't anything you can do about it. If this happens your program should shut down and provide as much information as possible about what happened.

Comments

1

I needed similar thing but in my case to always cleanup some resources when no exception occurred. The below solution example worked for me and should also answer the question.

    caught_exception=None
    try:
      x = 10/0
      #return my_function()
    except Exception as e:
      caught_exception = e
    finally:
      if caught_exception:
         #Do stuff when exception
         raise # re-raise exception
      print "No exception"

2 Comments

You'd better move #Do stuff when exception to except, and print "No exception" to else
@GingerPlusPlus I see your point, but the construct answers the question "But how can I catch and print the message in a 'finally' clause?"

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.