Python programming language provides programmers a huge number of exception handler libraries that help them to handle different types of exceptions.
One such exception is ValueError exception, let's understand it in detail.
ValueError Exception in python occurs when the variable is fed with wrong value or the value which is out of the expected bound. This is similar to type error which looks for wrong type instead.
Here is an example that will help you to learn more about it, suppose you are taking marks of the student as input from users. If the user enters strings it is treated as a typeError but if the user enters a value greater than 100, it is treated as a valueError.
Program to illustrate ValueError in Python
Output:
Explanation:
In the above code, we have created two variables num1 and num2 and taken user input for their values. Both the values entered need to be integer values only. And then we have printed their sum. The exception we have handled here is valueError but we have not put any upper limit to the value of the integer to be entered, and by default you can create an integer as big as you want in python. And hence, if we enter a non-integer value the ValueError exception returns the exception statement.
Example 2:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer