Alex Rivera | Logout

Function failed: Raise Exception, or return FALSE? What's the better approach?

Asked 2012-02-18T18:57:08.220
15

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!

Edit
Report

1 Answer

6

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.

answered 2012-02-18T19:01:22.317

Your Answer