0

I want to catch and handle a specific Exception and I want all others to be raised.

The exception I want to catch is like:

Exception("exception want to catch")

The code I tried below doesn't work. In the first code I want the exception to be raise;

try:
    raise Exception("exception don't want to catch")
except Exception("exception want to catch"):
    pass

But I wouldn't want an exception raise for this code:

try:
    raise Exception('exception want to catch')
except Exception('exception want to catch'):
    pass
4
  • 1
    Sounds like this could be an XY problem to me- else you just need to give more detail one what you are actually trying to do with this. Commented May 21, 2014 at 13:45
  • I would recommend you to create you own exception derived class and make an except for it and pass it. Commented May 21, 2014 at 13:46
  • Different types of error (e.g. ValueError vs. TypeError) or the same type of error with a different message? Did you write the code that raises the error, too? Commented May 21, 2014 at 13:48
  • Okay, but this isn't my code. I am using a module which throws an Exception('like this'). Commented May 21, 2014 at 13:48

2 Answers 2

3

You should define (or use preexisting) concrete exception classes instead of relying on the strings:

>>> class ExcToCatch(Exception): pass
... 
>>> class ExcToNotCatch(Exception): pass
... 
>>> try:
...     raise ExcToCatch()
... except ExcToCatch:
...     pass
... 
>>> try:
...     raise ExcToNotCatch()
... except ExcToCatch:
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
__main__.ExcToNotCatch

Presumably ExcToCatch and ExcToNotCatch mean something more meaningful, so they should be named appropriately.


If you absolutely must rely on the strings, you can obtain the string via str(exception):

>>> try:
...     raise Exception('some string')
... except Exception as e:
...     print str(e)
... 
some string

You can include some logic in the except block to re-raise e when necessary (e.g. when str(e) != 'exception want to catch').

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

2 Comments

This is exactly the outcome which I want. But is there no way to do it without making exception classes?
@WillBeauchamp there are many built-in exception classes - there is often one close enough to your use case to use.
1

If this exception derives from BaseException (which it seems like it's actually an instance of Exception) you can check the args property. The first should be the string you want to handle:

try:
  #stuff
catch Exception as ex:
  if ex.args[0] == 'My Exception string':
    #do stuff
  else:
    raise

That said, if you have any control over this library, please go and find the author to have him change it. If you do not, my condolences.

Comments

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.