A Value error is
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value
the float function can take a string, ie float('5'), it's just that the value 'string' in float('string') is an inappropriate (non-convertible) string
On the other hand,
Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError
so you would get a TypeError if you tried float(['5']) because a list can never be converted into a float.
Cite
Answer from David on Stack OverflowIn this example:
x = int(input("x: "))
print(f'x = {x}')if the input is a string, a ValueError is raised. Why not a TypeError? An invalid data type is inputted after all.
A Value error is
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value
the float function can take a string, ie float('5'), it's just that the value 'string' in float('string') is an inappropriate (non-convertible) string
On the other hand,
Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError
so you would get a TypeError if you tried float(['5']) because a list can never be converted into a float.
Cite
ValueError a function is called on a value of the correct type, but with an inappropriate value
TypeError : a function is called on a value of an inappropriate type
Value error, name error, type error?
ValueError vs TypeError
ValueError vs TypeError
python - ValueError Versus TypeError, Why? - Stack Overflow
What is the difference between a TypeError and a ValueError?
How do I handle errors in Python?
What are common Python errors?
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.
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.