Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Catching specific exception

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.

like image 802
José Avatar asked Nov 23 '12 14:11

José


People also ask

How can you catch a specific type of exception in Python?

Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.

How does Python handle unknown exceptions?

Try and Except Statement – Catching all Exceptions Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.

How do you catch multiple exceptions in Python?

By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.

How do you catch errors in Python?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.


1 Answers

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.

like image 61
mgilson Avatar answered Sep 29 '22 07:09

mgilson