@Liam Hayes (https://teamtreehouse.com/liamhayes) Hi! I have a note about @Steven Parker (https://teamtreehouse.com/stevenparker)'s example (especially the second one). When I was trying to wrap my head around this, I could not for the life of me understand why the built-in function for changing something to an int would give a ValueError with a string as opposed to a TypeError. Surely, it's expecting a number, right? Well, no. It isn't. The int() function takes either a number or a string. It can convert the string "21" to the integer 21, but it can't convert "dog" to a number. Thus, it's getting the right type, but it's still not a value that it can convert. This is when we get the ValueError. It's of the right type, but the value is such that it still can't be done. That being said, if we tried to send in something that was neither a string nor a number, it would have generated a TypeError as it is of the wrong type. Hope this helps! :sparkles: edited for additional note In your own custom made functions, you can pass in whatever you want, although ideally, you know what you're passing in. This is not true of built-in functions in Python. When in doubt, check the documentation for the function in question. Answer from Jennifer Nordell on teamtreehouse.com
Top answer
1 of 2
8
@Liam Hayes (https://teamtreehouse.com/liamhayes) Hi! I have a note about @Steven Parker (https://teamtreehouse.com/stevenparker)'s example (especially the second one). When I was trying to wrap my head around this, I could not for the life of me understand why the built-in function for changing something to an int would give a ValueError with a string as opposed to a TypeError. Surely, it's expecting a number, right? Well, no. It isn't. The int() function takes either a number or a string. It can convert the string "21" to the integer 21, but it can't convert "dog" to a number. Thus, it's getting the right type, but it's still not a value that it can convert. This is when we get the ValueError. It's of the right type, but the value is such that it still can't be done. That being said, if we tried to send in something that was neither a string nor a number, it would have generated a TypeError as it is of the wrong type. Hope this helps! :sparkles: edited for additional note In your own custom made functions, you can pass in whatever you want, although ideally, you know what you're passing in. This is not true of built-in functions in Python. When in doubt, check the documentation for the function in question.
2 of 2
0
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. Examples: passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError, but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError. For more details, see the Exceptions page (https://docs.python.org/3/library/exceptions.html) of the Python documentation.
🌐
Reddit
reddit.com › r/learnpython › what's the difference between a typeerror and valueerror?
r/learnpython on Reddit: What's the difference between a TypeError and ValueError?
September 21, 2022 -

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.

Discussions

ValueError and TypeError in python - Stack Overflow
I can't completely understand the difference between Type and Value error in Python3x. Why do we get a ValueError when I try float('string') instead of TypeError? shouldn't this give also a TypeE... More on stackoverflow.com
🌐 stackoverflow.com
Value error, name error, type error?
Minseok Kim is having issues with: What are these? More on teamtreehouse.com
🌐 teamtreehouse.com
1
September 22, 2018
ValueError vs TypeError
Eva Feng is having issues with: I find it a bit confusing to distinguish between ValueError and TypeError. For example, when you have float( a string ) - it shows up as ValueEr... More on ecs.teamtreehouse.com
🌐 ecs.teamtreehouse.com
1
February 26, 2026
ValueError vs TypeError
Eva Feng is having issues with: I find it a bit confusing to distinguish between ValueError and TypeError. For example, when you have float( a string ) - it shows up as ValueEr... More on teamtreehouse.com
🌐 teamtreehouse.com
1
February 26, 2026
People also ask

What are common Python errors?
The most common Python errors consist of SyntaxError, NameError, TypeError, ValueError, IndexError, KeyError, and AttributeError.
🌐
middleware.io
middleware.io › blog › python-error-types
Python Error Types: Common Errors and How to Handle Them
How do I handle errors in Python?
You should use the try-except blocks and write clear error messages.
🌐
middleware.io
middleware.io › blog › python-error-types
Python Error Types: Common Errors and How to Handle Them
How can I monitor Python errors in real time?
You can use tools like Middleware to track, analyze, and fix Python errors across your application instantly.
🌐
middleware.io
middleware.io › blog › python-error-types
Python Error Types: Common Errors and How to Handle Them
🌐
W3Schools
w3schools.com › python › ref_exception_valueerror.asp
Python ValueError Exception
Python Examples Python Compiler ... Study Plan Python Interview Q&A Python Training ... The ValueError exception occurs if a function receives a value of wrong type....
🌐
Carleton University
cs.carleton.edu › cs_comps › 1213 › pylearn › final_results › encyclopedia › valueError.html
Error Encyclopedia | Value Error
To encounter a ValueError in Python means that is a problem with the content of the object you tried to assign the value to. This is not to be confused with types in Python. For instance, imagine you have a dog and you try to put it in a fish tank. This would be an example of a type error, ...
🌐
Quora
quora.com › What-is-the-difference-between-TypeError-and-ValueError-in-Python-When-does-Python-throw-these-exceptions
What is the difference between 'TypeError' and 'ValueError' in Python? When does Python throw these exceptions? - Quora
Answer: 🔴1. TypeError happens when an operation is performed on an inappropriate type. For example: Copy [code]result = 'string' + 5 # Can't add string and integer [/code]🔴2. ValueError occurs when a function gets an argument of the right type but inappropriate value. For instance: Copy [co...
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 2497149 › what-is-the-difference-between-typeerror-and-valueerror
What is the difference between 'TypeError' and 'ValueError'? | Sololearn: Learn to code for FREE!
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 ...
🌐
Real Python
realpython.com › ref › builtin-exceptions › valueerror
ValueError | Python’s Built-in Exceptions – Real Python
ValueError is a built-in exception that gets raised when a function or operation receives an argument of the correct type, but its actual value isn’t acceptable for the operation at hand, and no more specific exception, such as IndexError, describes the situation.
🌐
Middleware
middleware.io › blog › python-error-types
Python Error Types: Common Errors and How to Handle Them
A TypeError happens when you use the wrong type (like adding a string to a number), while a ValueError happens when the type is right but the value is incorrect (like converting “abc” to an integer).
🌐
YouTube
youtube.com › shorts › ARBuge-5gtY
Python 3 | Type vs Value Errors, whats the difference? #programming #python - YouTube
Lets take a look at the difference between the commonly seen ValueError and TypeError
Published: January 16, 2026
🌐
X
x.com › reuvenmlerner › status › 1515790734208684037
Reuven M. Lerner on X: "I've always intuitively known the difference between TypeError and ValueError in #Python, but couldn't articulate it. Turns out the docs say it clearly: - TypeError means you passed an inappropriate type. - ValueError means you passed the right type, but an inappropriate value." / X
April 17, 2022 - I've always intuitively known the difference between TypeError and ValueError in #Python, but couldn't articulate it. Turns out the docs say it clearly: - TypeError means you passed an inappropriate type. - ValueError means you passed the right ...
🌐
Treehouse
ecs.teamtreehouse.com › community › valueerror-vs-typeerror
ValueError vs TypeError (Example) | Treehouse Community
February 26, 2026 - In this case, it's not the type causing the issue but the value. For example: float("boat") will return a ValueError, but float("42") returns 42.0 (and no error).
🌐
Scaler
scaler.com › home › topics › python valueerror
Python ValueError - Scaler Topics
December 15, 2022 - As shown in the above code snippet, ... therefore the python raises a ValueError. ... As discussed above, Value Error in python occurs when a correct argument type but an incorrect value is supplied to a function....
🌐
Honeybadger
honeybadger.io › blog › errors-in-python
Errors in Python: Types, Causes, and Examples - Honeybadger Developer Blog
April 27, 2026 - The TypeError exceptions in Python occur when an operation is applied to a value or variable of an incompatible data type. For example, adding an integer and a string results in a TypeError exception with the error message TypeError: unsupported ...
Top answer
1 of 2
6

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.

2 of 2
1

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

🌐
Accuweb
accuweb.cloud › home › explain python valueerror exception handling (with real examples & best practices)
Explain Python ValueError Exception Handling (With Real Examples & Best Practices)
January 15, 2024 - Python ValueError is raised when a function receives a value of the correct type but an inappropriate or invalid value. Example: Example: int(“abc”) # Raises ValueError The string is a valid type (str), but its value cannot be converted ...
🌐
Python
docs.python.org › 3 › builtins › exceptions.html
Built-in Exceptions — Python 3.14.7 documentation
Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError, but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError.