+ 2
What is the use of raising exception in python or program ?
3 ответов
+ 3
To give the programmer a clear signal of what went wrong, and to stop execution of the program, so that nothing happens that shouldn't.
Imagine you write a set of functions that other people will import and use in their code.
They can't (or don't want to) read your source code, but when something goes wrong, they want to know what and why.
Like when you try to do...
'5'/2
... Python will tell you: 'You can't divide a string by an int.'
It's convenient, right?
And you can give the same convenience to the users of your code.
Who might be yourself, actually, because when you write a very large program, you'll forget, how exactly you have written some function, because it was weeks ago.
+ 2
Thanks
+ 1
has a lot of advantages:
1- to make your code more understandable by others
2- to get certain values from users e. g: if your app need number and user entered string
...etc you've to check them online