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

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.help
Subject Re: Some same exceptions used in a given file
Date 2011-04-24 09:40 -0400
Organization albasani.net
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>

Show all headers | 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