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 OverflowA 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 - TypeError vs ValueError when trying to unpack a set - Stack Overflow
What is the difference between a TypeError and a ValueError?
How do I handle errors in Python?
What are common Python errors?
In 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 string is a sequence too; you can unpack a string into separate characters:
>>> a, b = 'cd'
>>> a
'c'
>>> b
'd'
The ValueError is raised because a string of length 1 cannot be unpacked into two targets.
When looping over a sequence of integers, however, you are trying to unpack each integer value into two targets, and integers are not iterable at all. That's a TypeError, because this is directly connected to the type of object you are trying to unpack:
>>> a, b = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
Compare this to strings, lists, tuples, iterators, generators, etc. which are all types that are iterable.
If you wanted to unpack the set directly, don't use a for loop:
>>> a, b = set([1, 2])
>>> a
1
>>> b
2
but do know that sets have no fixed order (just like dictionaries), and in what order the values are assigned depends on the exact history of insertions into and deletions from the set.
Your for loop is possibly going deeper than you expect. You're trying to unpack individual members of the set, not the set itself. In the first example the member is 1 and the second the member is a. Neither has two values.
What you possibly want is one, two = s.
You can also find this out by writing:
>>> s = set([1, 2])
>>> for one_two in s:
... print one_two
1
2
Python exceptions can be caught in this way:
try:
<your code>
except <Exception>:
<CODE 2>
OR LIKE THIS
try:
<your code>
except(<exception1>,<exception2>):
<Code to handle exception>
You are simply handling multiple exceptions together. You can always split them. They are not 2 different ways. In your case the as is for logging it .
Here are a few examples:
try:
<code>
except TypeError:
<Code for handling exception>
except ValueError:
<Code for handling exception>
except ValidationError:
<Code for handling exception>
except:
<Code for handling exception>
In the last case it catches exception of any type since no type is specified.
In Python programs can raise any exception for anything.
In fact exception is just a special class, even you can create one for your library.
So the best way to find about the exception is to read the docs of the library not the exception class.
If your program catches the exception and wants more detail about it for creating a log file the code can be written like this.
except TypeError as e:
i=str(e)
In this case you are catching the exception and converting its detail to a string.
This is from the Django docs about the error which you are talking about.
Form validation happens when the data is cleaned. If you want to customize this process, there are various places to make changes, each one serving a different purpose. Three types of cleaning methods are run during form processing. These are normally executed when you call the is_valid() method on a form. There are other things that can also trigger cleaning and validation (accessing the errors attribute or calling full_clean() directly), but normally they won’t be needed.
In general, any cleaning method can raise ValidationError if there is a problem with the data it is processing, passing the relevant information to the ValidationError constructor. See below for the best practice in raising ValidationError. If no ValidationError is raised, the method should return the cleaned (normalized) data as a Python object.
Some further references:
The link to docs
This link has info about other common builtin exception classes.
They are different blocks of code for handling different exceptions.
However in this example, both cases have the same logic for how they handle each exception.
It might make more sense if we split up the cases into 3 different blocks of code:
except TypeError as error:
LOGGER.error('Type error: ', exc_info=True);
except ValueError as error:
LOGGER.error('Value error: ', exc_info=True);
except ValidationError error:
LOGGER.error('Validation error: ', exc_info=True);
TypeError will be thrown when an incorrect type is used
ValueError will be thrown when an incorrect value is used
ValidationError will be thrown when the validation fails
The program will handle each exception differently