Why should we "raise" exceptions manually? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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 .

3rd Aug 2019, 12:51 PM
Sagar Hiremath
12 Réponses
+ 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. ūüėú)
3rd Aug 2019, 1:02 PM
HonFu - avatar
+ 20
Automatic exceptions mostly manage syntax errors. Manual raising of exceptions can be for semantic errors.
3rd Aug 2019, 11:45 PM
Sonic - avatar
+ 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 ...
6th Aug 2019, 11:48 AM
Sagar Hiremath
+ 7
ūüėÄ tq brooo
3rd Aug 2019, 1:04 PM
Sagar Hiremath
+ 6
Because we can be more specific on what condition you need to be as the triggering condition ūü§ó
4th Aug 2019, 3:30 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 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
4th Aug 2019, 6:29 AM
Mohammad Milad
Mohammad Milad - avatar
+ 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
6th Aug 2019, 8:06 AM
Ma√ģtre George
Ma√ģtre George - avatar
+ 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.
14th Aug 2021, 8:36 PM
‚ÄéŔÖō≠ŔÖōĮ Ŕäō≥ŔÜ ōĻŔÖōĪ‚Äé
‚ÄéŔÖō≠ŔÖōĮ Ŕäō≥ŔÜ ōĻŔÖōĪ‚Äé - avatar
+ 1
For greater customization ....
22nd Mar 2021, 11:20 AM
Sanjay Kamath
Sanjay Kamath - avatar
Are there any alternative of raise function?? ūüôĄ
16th Dec 2021, 3:51 PM
Shahrear Al Sakib
Shahrear Al Sakib - avatar
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.
26th Feb 2022, 9:21 AM
Jaya - avatar