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


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

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-03-31 19:57 -0400
Organization albasani.net
Message-ID <in34c7$r7$1@news.albasani.net> (permalink)
References (7 earlier) <845bd06b-b2c4-4c57-896c-a8d2347c7c75@m7g2000vbq.googlegroups.com> <in0tkg$7kv$1@news.albasani.net> <ab562d4f-9f21-4b87-b5ee-8ff0ed0bd556@w6g2000vbo.googlegroups.com> <8vjj4oFn46U1@mid.individual.net> <cadac90c-5beb-4769-96c7-34307d5eb552@e9g2000vbk.googlegroups.com>

Show all headers | View raw


Eric wrote:
> Interesting you can crash that simple program by passing in null
> instead of a String and Eclipse doesn't mention it.

That's not Eclipse's fault.  You failed to tell it to report null pointer 
problems.

> So should every such error be thrown or trapped?  It would seem such

Neither.  It should be prevented.

> error should be tracked down in the object where this method is
> called, and trapping them all would be a real nuisance.
>       public void foo( String sometxt )
>       {
>         if (sometxt == null) {
>              throw new RuntimeException("That's a null not a String!");

That achieves exactly nothing.  NPE already is a RuntimeException.  Instead of 
throwing a stone through the window you're throwing a baseball.  Either way 
you have broken glass.

>         }
>         System.out.println( "Input length "+ sometxt.length()
>            +" value\n\""+ sometxt +'"');
>       }
>
> Which of these makes more sense?
>       public void foo(String fileName) throws Exception
>       {
>         try {
>           File myFile = new File(fileName);
>           myFile.createNewFile();
>         }
>         catch (Exception e)
>         {
>           throw e;
>         }
>       }
>
>       public int foo(String fileName)
>       {
>         int returnValue = 0;
>         try {
>           File myFile = new File(fileName);
>           myFile.createNewFile();
>         }
>         catch (Exception e)
>         {
>           returnValue = 1;
>         }
>         return returnValue;
>       }
>
> Aside from tossing out numbers, the alternative to throwing the errors
> would seem to be tracking down every possible reason for the error and
> passing out..more numbers, or message strings?

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>

Item 57: Use exceptions only for exceptional conditions
Item 58: Use checked exceptions for recoverable conditions and runtime 
exceptions for programming errors
Item 60: Favor the use of standard exceptions

*Item 62: Document all exceptions thrown by each method*

*Item 65: Don't ignore exceptions*

Buy that book and study it.

  public void foo( String sometxt )
  {
    System.out.println( "Input length "+
      (sometxt == null? 0 : sometxt.length())
       +" value\n\""+ sometxt +'"' );
  }

-- 
Lew

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next 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