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?
2 odpowiedzi
+ 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.
+ 2
def sqrt(n):
if not isinstance(n, numbers.Number):
raise TypeError
if n < 0:
raise ValueError
...