0

Python Raise function

Hello, I just start to learn python. I follow the course explaining “raise” to manage the error. But it still not clear for what is useful, when it is good to use. If somebody can gives me more explanation about it, this will be very great.

5th Jan 2020, 11:29 PM
Ludovic Bourdon
2 ответов
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)
5th Jan 2020, 11:55 PM
HonFu
HonFu - avatar
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
6th Jan 2020, 1:13 AM
Gabriel Pedroza
Gabriel Pedroza - avatar