Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew 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: References: <87r58s28w3.fsf@merciadriluca-station.MERCIADRILUCA> <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 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