+ 25
Why should we "raise" exceptions manually?
I don't understand that , when something went wrong in code then python automatically raise the error and stops the program ,so what is the nesserray to raise expetions by manully .
13 Antworten
+ 127
When you write a function, that other people are going to use, they usually don't (want to) read the code.
They just want to read the docstring and find out how to use the function.
And when they make a mistake, they want to get a clear error message.
raise allows you to raise one of the built-in exceptions and add more information to it. Also you can define your own exceptions.
It also works in cases where Python would NOT raise an error. For example you could write:
if '[' in that_string:
raise ValueError
(Python wouldn't care if a string contains a [ or not.)
So the user of your function may get something like:
YouMessedUpException 'Seriously, dude?'
(Or something slightly more helpful. 😜)
+ 20
Automatic exceptions mostly manage syntax errors. Manual raising of exceptions can be for semantic errors.
+ 11
Maître George u are in begining of course ,u will learn about expections in upcoming lessons .
Whatever I clarify about the ans what I understood that
Name=input ("enter ur name=")
if '1' in Name:
raise TypeError
In output window
enter ur name= 123
Traceback (most recent call last):
File "________",line 3, in<module>
raise TypeError
TypeError
Here input is = 123
But I don't want this type of input which have numbers . So when I got the number like 1 in input , i emidditly raise the type error.so we can raise any type of errors when we don't want some type of stuff as a input , and in many cases toooooo. I thought it is helpful to someone .
If I am wrong any where then tell me ,Ur all comments are welcomed ..........thank u and sorry for my bad English ...
+ 7
😀 tq brooo
+ 6
Because we can be more specific on what condition you need to be as the triggering condition 🤗
+ 2
If you use a library you'll find out that if you pass a wrong argument you get an error that why we use errors
+ 2
i don't even know what's going on here.
i don't even understand how I find myself here and what I should write
+ 2
if you have a program for example take inputs from users as a gmail yes while you are programming you don't see as user see, you give them the gist in a clear and tidy program not like you see when you was programming if you was in interpreter or like that. you make the program simple for them, but the excellence followed by problem which concomitant the inputs will harm your program. without raise, try, except, finally the program will stop because have not control about the exception .
Remember if exception raised the code will break and for this reason raise available.
+ 1
For greater customization ....
0
Are there any alternative of raise function?? 🙄
0
Your questions is right, although python do it automatically but we should also be able to figure out and understand the problems that's why it's just a way to understand problem solving in a better way. We can first do it manually with pen-paper and then check out in python for the desired result.