Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python style: exceptions vs. sys.exit()

Reply
Thread Tools

Python style: exceptions vs. sys.exit()

 
 
Drake
Guest
Posts: n/a
 
      09-23-2008
I have a general question of Python style, or perhaps just good
programming practice.

My group is developing a medium-sized library of general-purpose
Python functions, some of which do I/O. Therefore it is possible for
many of the library functions to raise IOError Exceptions. The
question is: should the library function be able to just dump to
sys.exit() with a message about the error (like "couldn't open this
file"), or should the exception propagate to the calling program which
handles the issue?

Thanks in advance for anyone who can either answer my question or
point me to where this question has already been answered.

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      09-23-2008
Drake wrote:
> I have a general question of Python style, or perhaps just good
> programming practice.
>
> My group is developing a medium-sized library of general-purpose
> Python functions, some of which do I/O. Therefore it is possible for
> many of the library functions to raise IOError Exceptions. The
> question is: should the library function be able to just dump to
> sys.exit() with a message about the error (like "couldn't open this
> file"), or should the exception propagate to the calling program which
> handles the issue?
>
> Thanks in advance for anyone who can either answer my question or
> point me to where this question has already been answered.
>


IMHO libraries should always just let the exception propagate up to the caller.
That allows the caller the option of taking the appropriate action.

-Larry
 
Reply With Quote
 
 
 
 
Craig Allen
Guest
Posts: n/a
 
      09-23-2008
> The
> question is: should the library function be able to just dump to
> sys.exit() with a message about the error (like "couldn't open this
> file"), or should the exception propagate to the calling program which
> handles the issue?
>


my view is that the exceptions are there precisely to tell the calling
program about the error and give the programmer a chance to do
something smart about it. A library, properly, doesn't even know the
context in which it is running, and sys.exit() is pretty heavy handed
for a library to call and shows assumptions beyond what a library
should assume about its running environment.

imho
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-23-2008
Drake wrote:
> I have a general question of Python style, or perhaps just good
> programming practice.
>
> My group is developing a medium-sized library of general-purpose
> Python functions, some of which do I/O. Therefore it is possible for
> many of the library functions to raise IOError Exceptions. The
> question is: should the library function be able to just dump to
> sys.exit() with a message about the error (like "couldn't open this
> file"),


No

> or should the exception propagate to the calling program which
> handles the issue?


Yes -- with an informative error message.

If the caller ignores the exception, the program will exit with a full
stack trace anyway.

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-24-2008
On Tue, 23 Sep 2008 13:25:26 -0700, Drake wrote:

> I have a general question of Python style, or perhaps just good
> programming practice.
>
> My group is developing a medium-sized library of general-purpose Python
> functions, some of which do I/O. Therefore it is possible for many of
> the library functions to raise IOError Exceptions. The question is:
> should the library function be able to just dump to sys.exit() with a
> message about the error (like "couldn't open this file"), or should the
> exception propagate to the calling program which handles the issue?
>
> Thanks in advance for anyone who can either answer my question or point
> me to where this question has already been answered.



Presumably somebody has suggested that calling sys.exit() was a good
option. I'm curious to what possible reason they could give for such a
poor choice.

Hint: if a library function can't open a file, the application should be
given the opportunity to open a different file. Or do something
completely different instead. Whatever. That's not up to the library to
decide, not even if the library is in such a broken state that it can't
continue. Why not? Because the application might deal with that by
unloading the library and continuing regardless.



--
Steven
 
Reply With Quote
 
Asun Friere
Guest
Posts: n/a
 
      09-24-2008
On Sep 24, 8:10 am, Christian Heimes <(E-Mail Removed)> wrote:
> Side note:
>
> sys.exit() is just another way to write raise SystemExit. The function
> is defined as:
>


As can be seen if you were ever silly enough to call sys.exit() in
IDLE.
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-24-2008
Drake a écrit :
> I have a general question of Python style, or perhaps just good
> programming practice.
>
> My group is developing a medium-sized library of general-purpose
> Python functions, some of which do I/O. Therefore it is possible for
> many of the library functions to raise IOError Exceptions. The
> question is: should the library function be able to just dump to
> sys.exit() with a message about the error (like "couldn't open this
> file"),


Arrghll ! NO, DONT !

> or should the exception propagate to the calling program which
> handles the issue?


Yes, by all means and for God's sake.

> Thanks in advance for anyone who can either answer my question or
> point me to where this question has already been answered.


There have been numerous threads about this here.

 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      09-24-2008
On Sep 23, 4:25*pm, Drake <(E-Mail Removed)> wrote:
> The
> question is: should the library function be able to just dump to
> sys.exit() with a message about the error (like "couldn't open this
> file"),


I'm kind of curious what your library is for. Is it something where
exiting the app be the only appropriate action for an IO error?

Even if it is, I will echo other people's advice: a library should
never call exit, at least not by default. For your imagined use it
might make sense to exit upon any failure, but other people using the
library might not want to do that.

For that matter, a library should never print error or status
messages. Messages should either be sent to the caller somehow, or
handled using the logging facility.


Carl Banks
 
Reply With Quote
 
Tim Rowe
Guest
Posts: n/a
 
      09-24-2008
2008/9/24 Bruno Desthuilliers <(E-Mail Removed)>:
> Drake a écrit :


>> many of the library functions to raise IOError Exceptions. The
>> question is: should the library function be able to just dump to
>> sys.exit() with a message about the error (like "couldn't open this
>> file"),

>
> Arrghll ! NO, DONT !


Can I put in a vote *for* the questioner's library dumping to
sys.exit() on any abnormal condition? It would reduce employment
competition for the rest of us.

Why, yes, I am wearing my BOFH hat. How could you tell?

--
Tim Rowe
 
Reply With Quote
 
Ross Ridge
Guest
Posts: n/a
 
      09-24-2008
Steven D'Aprano <(E-Mail Removed)> wrote:
> Presumably somebody has suggested that calling sys.exit() was a good
> option. I'm curious to what possible reason they could give for such a
> poor choice.


Grant Edwards <invalid@invalid> wrote:
>Same here. It's like an automotive engine controls designer
>asking if a failed O2 sensor should turn on the check engine
>light or blow up the car.


No, it's more like asking if the failed sensor should turn on a strange
and mysterious light on the dashboard and then blow up the car if the
driver doesn't immediately stop and check the engine. The owners manual
would only vaguely hint at the fact that this could happen.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] http://www.velocityreviews.com/forums/(E-Mail Removed)
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Fatal Python error using ctypes & python exceptions mmacrobert Python 1 08-31-2007 03:55 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments