Python: Catching specific exception

PythonException

Python Problem Overview


I want to catch a specific ValueError, not just any ValueError.
I tried something like this:

try: maquina['WPF'] = macdat(ibus, id, 'WPF')
except: ValueError, 'For STRING = ’WPF’, this machine is not a wind machine.':
    pass

But it raises a SyntaxError: can't assign to literal.
Then I tried:

try: maquina['WPF'] = macdat(ibus, id, 'WPF')
except ValueError, e:
    if e != 'For STRING = ’WPF’, this machine is not a wind machine.':
        raise ValueError, e

But it raises the exception, even if it is the one I want to avoid.

Python Solutions


Solution 1 - Python

in except ValueError,e, e is an instance of the exception, not a string. So when you test if e is not equal to a particular string, that test is always False. Try:

if str(e) != "..."

instead.

Example:

def catch(msg):
    try:
        raise ValueError(msg)
    except ValueError as e:  # as e syntax added in ~python2.5
	    if str(e) != "foo":
	        raise
        else:
	        print("caught!")

catch("foo")
catch("bar")

Typically, you don't really want to rely on the error message if you can help it -- It's a little too fragile. If you have control over the callable macdat, instead of raising a ValueError in macdat, you could raise a custom exception which inherits from ValueError:

class MyValueError(ValueError): pass

Then you can only catch MyValueError and let other ValueErrors continue on their way to be caught by something else (or not). Simple except ValueError will still catch this type of exception as well so it should behave the same in other code which might also be catching ValueErrors from this function.

Solution 2 - Python

The method for the last one is correct (but print repr(e) to see why it doesn't work).

However, if you want the exception information to be correct, you should not raise a new exception (as you do now), but raise the same one. Otherwise more code catching it, or the error message if it isn't caught, will show your code as the source, while it should be the original source.

To do this, use raise without an argument (within the except block, of course, otherwise there is no "current" exception).

Solution 3 - Python

You can use: type(e) and e.args for this. It returns a tuple, match the tuple with your own.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoséView Question on Stackoverflow
Solution 1 - PythonmgilsonView Answer on Stackoverflow
Solution 2 - PythonBas WijnenView Answer on Stackoverflow
Solution 3 - PythonSiddharth MishraView Answer on Stackoverflow