2 Réponses
0
Imagine two people work in a company.
Person A writes a function.
Person B is supposed to use that function but can't/doesn't have time to read the code.
Example: Function that returns: prime or not.
def prime(n):
if n<2:
raise ValueError('Only values >= 2!')
# Here would be the code
Now if A uses the function with a value that's too low...
Well try it, for example like this:
prime(1)
0
Imagine you're programming a calculator, for example. If someone enters a letter, it will crash into an error. The person using it may not know the reason for it, so they might think your code is broken. With the 'raise' function, you can put the input section into a try/except structure, and in the "except" block you can raise the error with a personalized message, helping the user find it's mistake. You can try bugging my integer-only program to understand it better:
https://code.sololearn.com/cm49LZ3lNhKH/?ref=app