Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #556

Re: When to throw exceptions and when to use System.err?

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.help
Subject Re: When to throw exceptions and when to use System.err?
Date 2011-04-06 12:40 -0400
Organization albasani.net
Message-ID <ini51p$8eh$1@news.albasani.net> (permalink)
References (9 earlier) <ab562d4f-9f21-4b87-b5ee-8ff0ed0bd556@w6g2000vbo.googlegroups.com> <in3409$em$1@news.albasani.net> <dbbd9bda-0dd4-4da8-a4f0-b03fa797a8f5@d2g2000yqn.googlegroups.com> <in5oer$bbr$1@news.albasani.net> <77b9d623-da08-45af-88ad-5f1b3fa4d623@s3g2000vbf.googlegroups.com>

Show all headers | View raw


Eric wrote:
 > Lew wrote:
>>> YES!  You absolutely MUST track down EVERY possible reason for error and prevent it.
>>> THAT'S PROGRAMMING!
>>> Why would you ever do otherwise?  Do you actually want to leave possibility for error in your program?
>>> Really?  Why?
>>> Again, don't catch exceptions, prevent them.  Do you recall that I posted upthread:
>>>
>>> <http://java.sun.com/docs/books/effective/toc.html>
>>
>> What part of that answer was not clear?

> The part where you said all possible errors should be caught and every
> method should exit normally passing out values for every possible
> error not throwing anything, then you say it's good to throw errors
> because if it were not good practice then the Java API would not do
> it, the java.io.File.read() would not crash if the file were
> unavailable or the program didn't have access to it, it would exit
> gracefully passing out a value to let your program know exactly what
> the problem is.

OK, please allow me to clarify, and correct any erroneous comment I made.

One can throw an exception from a method, of course.  I didn't say that was 
bad.  I did say one should not let an exception crash a *program*.

To wit:
> Catch it, log it, and restore the program to a sane state.
> Sometimes that means rethrowing the exception, but very rarely.  More common
> is to recast into an application catch-all exception.  More common, or better
> practice in more situations, is to provide an alternate return from the
> operation to signal to the caller that there was a problem - that's where a
> buried exception can be rethrown sometimes.  It's a matter of art.

Note:
- "restore the *program* to a sane state" - not the method
- I did endorse throwing exceptions from methods
- I described several options
- I made no absolute statements other than EVERY exception must be handled - 
again, I'm not saying don't rethrow or wrap-and-throw, but then /that/ 
exception must be handled so that the *program* gets unexceptional.

Is that clearer?

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.help | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: When to throw exceptions and when to use System.err? David Lamb <dalamb@cs.queensu.ca> - 2011-03-29 07:29 -0400
  Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-29 11:21 -0400
    Re: When to throw exceptions and when to use System.err? David Lamb <dalamb@cs.queensu.ca> - 2011-03-29 18:38 -0400
      Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-29 18:53 -0400
        Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-03-30 11:48 -0700
          Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-30 23:49 -0400
            Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-30 23:52 -0400
            Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-03-31 06:55 -0700
              Re: When to throw exceptions and when to use System.err? Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-03-31 15:58 +0100
                Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-03-31 09:02 -0700
                Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-31 19:57 -0400
              Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-03-31 19:50 -0400
                Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-04-01 10:44 -0700
                Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-04-01 19:52 -0400
                Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-04-04 08:47 -0700
                Re: When to throw exceptions and when to use System.err? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-04-04 13:40 -0400
                Re: When to throw exceptions and when to use System.err? Eric <e.d.programmer@gmail.com> - 2011-04-04 11:56 -0700
                Re: When to throw exceptions and when to use System.err? Lew <noone@lewscanon.com> - 2011-04-06 12:40 -0400

csiph-web