I was wondering how you guys handle functions fails. Do you raise an exception, or do you return an error message?
e.G. I have a function that is supposed to connect to an external com-object. If the com-object has not been initiated through another program, the connection cannot be established. What would be the preferred python way of notifying the main program? Should I raise an exception with the detailed error message, or should I simple return the error message?
Thanks!
Throw an exception, this is what they are there for.
They allow the things using your code to manage the error, passing back strings provides too much opportunity for mishandling.
Consider the case you return a string or an iterable normally, checking for an error message could cause problems, and may not catch them all the time, it's also not very Pythonic.
python absolutely comes down on the side of exceptions here. i have always found this article to be a great explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With