Function Error: Raise Exception or FALSE? What is the best approach?

I was wondering how you guys cope with functions that fail. Are you throwing an exception or returning an error message?

eg. I have a function that needs to connect to an external com object. If the com object was not initiated using another program, the connection cannot be established. What would be the preferred python way to notify the main program? Should I throw an exception with a detailed error message, or should I just return an error message?

Thanks!

+7
source share
2 answers

python absolutely comes down to exceptions here. I always found this article to be a great explanation.

+9
source

Throw an exception, this is what they exist for.

They allow you to use the code to control the error, passing back lines provides too many opportunities for improper handling.

Consider the case when you return a string or iteration in normal mode, checking the error message can cause problems and cannot constantly intercept them, it is also not very Pythonic.

+2
source

All Articles