KBD

Keith Devens .com

Saturday, November 22, 2008 Flag waving
And if you go too far up, abstraction-wise, you run out of oxygen. Sometimes smart thinkers just don't... – Joel Spolsky
← Neat article at SlateGames Democrats play →

Daily link icon Tuesday, September 16, 2003

Exceptions vs. status returns

Ned Batchelder: Exceptions vs. status returns

Broadly speaking, there are two ways to handle errors as they pass from layer to layer in software: throwing exceptions and returning status codes. Almost everyone agrees that exceptions are the better way to do it, but some people still prefer status returns. This article shows why exceptions are better.

I've been thinking about the issue some lately, as I've been annoyed that Python's (and Ruby's... haven't tried Perl's for this) FTP library throws exceptions in places where just a return value might be easier to deal with... For instance, I want to change to a directory on an FTP server, but if the directory doesn't exist, create it. To me, something like:

if not ftp.cwd(settings['remote-dir']):
    print "Couldn't change to directory (",e,").
        Making directory",settings['remote-dir']
    if not ftp.mkd(settings['remote-dir']):
        print "Error, couldn't make directory:", e
        ftp.close()
        sys.exit(1)
    else:
        ftp.cwd(settings['remote-dir'])

is much easier than:

try:
    print ftp.cwd(settings['remote-dir'])
except ftplib.error_perm, e:
    try:
        print "Couldn't change to directory (",e,").
            Making directory",settings['remote-dir']
        ftp.mkd(settings['remote-dir'])
    except e:
        print "Error, couldn't make directory:", e
        ftp.close()
        sys.exit(1)
    else:
        ftp.cwd(settings['remote-dir'])

(code reformatted a bit to fit)

Not a great example, and maybe exceptions are better even in this case because they'll kill your program if the second "cwd" fails so you won't be working in an inconsistent state.

Anyway, just thinking out loud... I'm not totally comfortable with exceptions since I didn't "grow up" using them. I'd write more or think more... but I gotta get to bed.

← Neat article at SlateGames Democrats play →

Comments XML gif


Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

November 2008
SunMonTueWedThuFriSat
 1
2345678
9101112131415
16171819202122
23242526272829
30 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 4 posts

Recent comments XML

Calif. Supreme Court to take up gay marriage ban

I would argue the point is not​definitional.  While the word​marriage is su...

Justin: Nov 20, 4:37pm

Java join function

Meh, don't have null strings in​your string arrays imo, but you're​welcome ...

Keith: Nov 19, 7:51pm

Girls, please don't get breast implants

sorry but another thing i have to​make a comment on about you​men...the men...

happynow: Nov 17, 11:36pm

Books by Vincent Cheung

to all Cheung​fans:

read:

http://www.progin​osko.com/aquascum/cheung.h...

Zamir: Nov 16, 9:07am

Generated in about 0.192s.

(Used 8 db queries)

mobile phone