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


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

Re: Some same exceptions used in a given file

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail
From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.help
Subject Re: Some same exceptions used in a given file
Date Sun, 24 Apr 2011 09:40:12 -0400
Organization albasani.net
Lines 55
Message-ID <ip197p$ej1$1@news.albasani.net> (permalink)
References <87r58s28w3.fsf@merciadriluca-station.MERCIADRILUCA> <x-Sdnf9tjIy5pi7QnZ2dnUVZ_vqdnZ2d@earthlink.com> <87ei4sm7b6.fsf@merciadriluca-station.MERCIADRILUCA>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.albasani.net u9VFdNEfF0RxBOcbfocHvySlsxqYMesYO9dGvm3QdT08n1cL+5JJ3gt6CewNladK/j4F+O7FG54w7KL3tpQCMUkiQ73PAnVNyNuuVG0/DPcSuVJrkU9evkaPuN7XDn6t
NNTP-Posting-Date Sun, 24 Apr 2011 13:40:09 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="lmUMCLHCF5xWgbwI1KdqFimhfkDwji3is7f7q3h5SUazzZc9Ag8ro6RsFnrtMGsQ3scyTis2wyQaC0lB2Posjl0nIXd20hl2uJN1bzIylcS6dLtbLpcD4udzkjSk40jA"; mail-complaints-to="abuse@albasani.net"
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8
In-Reply-To <87ei4sm7b6.fsf@merciadriluca-station.MERCIADRILUCA>
Cancel-Lock sha1:wqD2ATainELasjkGXDWjjfuBop0=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:636

Show key headers only | View raw


Merciadri Luca wrote:
> Patricia Shanahan writes:
>> I can see two options for reducing the amount of duplicate code:
>>
>> 1. Put the try-catch for IOException at a high enough level in the call
>> hierarchy, and wide enough scope, that it will catch all instances of
>> IOException for which you want the same handling.
>>
>> 2. Extract the handling for IOException into a method that can be called
>> from multiple catch blocks.
>
> And what is the difference between try and catch at a high enough
> level and the same, but with a throw in the method that might generate
> an exception?

The difference is where they fit in your strategy.  The catch at the "high 
enough" level will not throw the lower-level exception (e.g., 'IOException'), 
but the lower-level routine will.  That's the difference.

"High enough" means at a layer where lower layers are subject to, for example, 
'IOException'.  At that point, where all lower layers log-and-rethrow the 
exception, the higher-level method will catch the exception and convert it 
into a legitimate interaction.

You *did* read my earlier post about exception strategy, interaction by 
contract and conversion thereto, did you not?

Take the advice you've been given and think about it.  You're not going to get 
all your insights spoon-fed to you via Usenet.  Programming is art that 
requires the practitioner *himself* to think.

So here's how that "higher-level" routine might look:

  public Status orchestrate( InputData data )
  {
    Status rval;
    try
    {
      somethingThatMightThrow( data );
      somethingElseThatRisksIt( data );
      yetSomethingElseThatCanThrow( data );
      rval = Status.SUCCESS;
    }
    catch ( IOException exc )
    {
      logger.error( LOWLEVELEXC, exc ); // suitable String
      rval = Status.SCREWUP;
    }
    return rval;
  }

-- 
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 | Next in thread | Find similar


Thread

Some same exceptions used in a given file Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-23 19:08 +0200
  Re: Some same exceptions used in a given file Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-23 14:40 -0400
    Re: Some same exceptions used in a given file Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-23 16:23 -0400
    Re: Some same exceptions used in a given file Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-04-23 17:23 -0300
    Re: Some same exceptions used in a given file Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-24 00:00 +0200
      Re: Some same exceptions used in a given file Lew <noone@lewscanon.com> - 2011-04-23 18:22 -0400
      Re: Some same exceptions used in a given file Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-23 18:31 -0400
        Re: Some same exceptions used in a given file Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-24 01:20 +0200
          Re: Some same exceptions used in a given file Lew <noone@lewscanon.com> - 2011-04-23 20:19 -0400
      Re: Some same exceptions used in a given file Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-04-23 22:25 -0300
        Re: Some same exceptions used in a given file Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-24 09:24 +0200
  Re: Some same exceptions used in a given file Patricia Shanahan <pats@acm.org> - 2011-04-23 13:48 -0700
    Re: Some same exceptions used in a given file Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-24 09:34 +0200
      Re: Some same exceptions used in a given file Lew <noone@lewscanon.com> - 2011-04-24 09:40 -0400
      Re: Some same exceptions used in a given file Patricia Shanahan <pats@acm.org> - 2011-04-24 06:53 -0700
  Re: Some same exceptions used in a given file Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-24 01:28 +0200

csiph-web