+ 40
Why raise exceptions in Python 3?
I know what "raise" does but couldn't figure out it utilisation. Why don't we just show whats wrong with "print" and terminate the program with "sys.exit()"? After all , the raise will too do the same. Wouldn't it be more neat to print and not raise an error? Thanks!
17 Respuestas
+ 58
Thing is, in programs errors are more useful than having the program just suddenly exiting
Let's say you are using a module, you accidentally use it wrong, and now your program shuts itself down🤷♂️
Consider this:
# my module code (name = Module)
def PrintFactorial(num):
try:
int(num)
except ValueError:
raise DataTypeError("Not integral data-type detected")
# my main code
import myModule
# will raise an error showing exactly what you did wrong
print(Module.PrintFactorial("a"))
+ 11
Python gives you that flexibility, but it is bad practice, and not generally accepted. Exceptions allow the program to contribute to execute, if you choose. Also, logging is preferred over print. However, it all comes down to preference and if that is what you choose to do then that is what you choose to do. I would keep in mind that others tend to use exceptions and logging rather than the print and sys exit
+ 10
It is a convention that a function must not have any "side effects"
In other words, it must not so something other than what it was designed to do. You expect it to print the factorial of an integer, not have it shut your whole program down if you accidentally made an error.
You do it for other programmers
+ 7
raise - enables you to create custom exceptions eg) salary < 2000 😃 This helps to create user defined exceptions which can be applied throughout the application, Error codes and messages stored in a database table, without clutter and unnecessary reputation.
+ 3
I personally do. It helps me keep track of what I did wrong. Especially in long one-file codes and in object oriented coding
+ 1
Thanks Thomas ! Do you use "raise" even for the basic codes you intend not to use as modules ?
+ 1
When a program running error occure then program isn't crash and raise exceptions
0
Nothing much hard
0
I think "raise" exist only for QA , cause I can build "if" or "while" construction for all exceptions, as a check what's happen.
0
Python comes with a robust error handling framework. Using structured exception handling and a set of pre-defined exceptions, Python programs can determine the error type at run time and act accordingly. These can include actions like taking an alternate path, using default values, or prompting for correct input.
0
I still don't know the importance of raise while writing codes
0
Is the raise only usable for debuging and testing?
0
Lot of healthy stuff
0
Yaa
0
It's better to raise a exception than just print a error message because your code can be called by a external module.
- 1
Exception : They occur when something goes wrong
Python has several built in exception such as,ZeroDivisionError.
Third party library also define their own exceptions
- 1
I have no idea, sorry