Why should we "raise" exceptions manually? | SoloLearn: Learn to code for FREE!

+10

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 .

8/3/2019 12:51:48 PM

Sagar Hiremath

9 Answers

New Answer

+22

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. 😜)

+9

Automatic exceptions mostly manage syntax errors. Manual raising of exceptions can be for semantic errors.

+6

😀 tq brooo

+6

Exceptions allow you to address things that could go wrong, like with user input. Say that a user enters an invalid value, or a letter, or some other character that isn't what you need. If you don't put in cases to handle this, unexpected things tend to happen

+5

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

https://code.sololearn.com/WA4GNuWfI8b0/?ref=app https://code.sololearn.com/cBc5B5dnk7BJ/?ref=app

+2

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 ...

+1

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