+ 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!

31st Jul 2019, 11:10 AM
Ayush Gupta
Ayush Gupta - avatar
17 Answers
+ 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"))
31st Jul 2019, 11:24 AM
Trigger
Trigger - avatar
+ 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
31st Jul 2019, 11:22 AM
Steven M
Steven M - avatar
+ 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
31st Jul 2019, 11:26 AM
Trigger
Trigger - avatar
+ 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.
8th Aug 2021, 3:07 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 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
31st Jul 2019, 11:43 AM
Trigger
Trigger - avatar
+ 1
Thanks Thomas ! Do you use "raise" even for the basic codes you intend not to use as modules ?
31st Jul 2019, 11:41 AM
Ayush Gupta
Ayush Gupta - avatar
+ 1
When a program running error occure then program isn't crash and raise exceptions
3rd Jan 2022, 12:24 AM
N. Vimukthi Dilshan Fernando
0
Nothing much hard
21st Mar 2022, 2:52 AM
Vikram Kumar
Vikram Kumar - avatar
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.
22nd Jun 2022, 4:59 AM
Roman Bernadskyy
Roman Bernadskyy - avatar
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.
22nd Jun 2022, 9:06 AM
Prathamesh Kalpande
Prathamesh Kalpande - avatar
0
I still don't know the importance of raise while writing codes
2nd Jul 2022, 5:47 PM
Emmanuel Okechukwu
Emmanuel Okechukwu - avatar
0
Is the raise only usable for debuging and testing?
12th Jul 2022, 2:18 PM
Ramtin Jafari
Ramtin Jafari - avatar
0
Lot of healthy stuff
5th Nov 2022, 6:44 PM
AIJAZ
0
Yaa
29th Jan 2023, 11:29 PM
ANIL KUMAR VIHSWAKARMA
ANIL KUMAR VIHSWAKARMA - avatar
0
It's better to raise a exception than just print a error message because your code can be called by a external module.
15th Feb 2023, 6:51 PM
José Marcos
José Marcos - avatar
- 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
11th Mar 2022, 7:14 PM
Prathamesh Kalpande
Prathamesh Kalpande - avatar
- 1
I have no idea, sorry
19th Dec 2022, 8:33 AM
Benjamin Fröhlich
Benjamin Fröhlich - avatar