TypeError and ValueError seem so similar to me (almost as if they were the same). Can someone please give me an example of both so I can know the difference? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

TypeError and ValueError seem so similar to me (almost as if they were the same). Can someone please give me an example of both so I can know the difference?

13th Sep 2016, 2:11 PM
DELETED
2 Answers
+ 3
TypeError is raised when you want to do something that's not supported by design. For example, it makes no sense to multiply two strings. ValueError is raised when type is okay, but the value itself makes no sense. Like calculating a square root from -1.
13th Sep 2016, 10:38 PM
trueneu
trueneu - avatar
+ 2
def sqrt(n): if not isinstance(n, numbers.Number): raise TypeError if n < 0: raise ValueError ...
13th Sep 2016, 2:46 PM
Zen
Zen - avatar