Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: difference between ValueError and Exception?

I am trying to understand what is a difference between raising a ValueError and an Exception. I have tried both in the same code (even in the same branch) and the result was the same - I got an error message.

I have made a research on this question on SO, but found no discussion on this. Then I read the documentation of exceptions, and found the following definition of ValueError:

Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.

So as I understand, an Exception is a more general term, and ValueError can be applied in some specific cases. But since the results of raising both things are the same, I want to understand, what is the practical meaning of differentiating between a ValueError and an Exception. Python version should be here not relevant. Thank you!

EDIT: Thanks to your answers I got it, what is the difference between both terms in try-exception construct. But how do they differ in case of just raising them, not excepting?

raise Exception('blah') 

and

raise ValueError('blah') 

Answering to @PeterWood: in both cases I just got the error message "blah", but in one case it was "Exception: blah", and in the second: "ValueError: blah". And I see in this case no practical difference between them both.

like image 561
vlad.rad Avatar asked Apr 25 '17 08:04

vlad.rad


People also ask

Is ValueError an exception Python?

The ValueError exception in Python is raised when the method receives the argument of the correct data type but an inappropriate value. The associated value is a string giving details about the data type mismatch.

What is a ValueError in Python?

What is Python ValueError? Python ValueError is raised when a function receives an argument of the correct type but an inappropriate value. Also, the situation should not be described by a more precise exception such as IndexError.

What is the difference between ValueError and type error?

A TypeError occurs when an operation or function is applied to an object of inappropriate type. A ValueError occurs when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.

What is the difference between error and exception in Python?

An Error might indicate critical problems that a reasonable application should not try to catch, while an Exception might indicate conditions that an application should try to catch. Errors are a form of an unchecked exception and are irrecoverable like an OutOfMemoryError , which a programmer should not try to handle.


2 Answers

ValueError inherits from Exception. You can decide to trap either only ValueError, or Exception, that's what exception inheritance is for.

In this example:

try:
    a=12+"xxx"
except Exception:
    # exception is trapped (TypeError)

exception is trapped, all exceptions (except BaseException exceptions) are trapped by the except statement.

In this other example:

try:
    a=12+"xxx"
except ValueError:
    # not trapped

Here, exception is NOT trapped (TypeError is not ValueError and does not inherit)

You generally use specific exceptions to trap only the ones that are likely to occur (best example is IOError when handling files), and leave the rest untrapped. The danger of catching all exceptions is to get a piece of code that does not crash, but does nothing.

(editing the answer in response to your edit:) when you raise an exception: you're creating an instance of Exception which will be filtered out by future except ValueError: statements. the message is different because the representation of the exception (when printed) includes the exception class name.

like image 167
Jean-François Fabre Avatar answered Oct 28 '22 17:10

Jean-François Fabre


You said it, ValueError is a specific Exception. A short example :

try:
    print int("hello world")
except ValueError:
    print "A short description for ValueError"

If you change "hello world" with an int, print int(42), you will not raise the exception.

You can see doc about exceptions here.

like image 33
Nicolas Flores Avatar answered Oct 28 '22 16:10

Nicolas Flores