Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 24 Apr 2011 08:53:15 -0500 Date: Sun, 24 Apr 2011 06:53:11 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.java.help Subject: Re: Some same exceptions used in a given file References: <87r58s28w3.fsf@merciadriluca-station.MERCIADRILUCA> <87ei4sm7b6.fsf@merciadriluca-station.MERCIADRILUCA> In-Reply-To: <87ei4sm7b6.fsf@merciadriluca-station.MERCIADRILUCA> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <_q6dnZ2mCMrWtinQnZ2dnUVZ_vOdnZ2d@earthlink.com> Lines: 48 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 75.8.126.96 X-Trace: sv3-YDwYx1kCYarWPrWF2r1xVMxSNBH/E8dfRelfKiOANK4inYXeYAyVC3LT1z73Nn9Ayq2xfXzYAUpmH6R!2QGWZQ0+WvBVnlawtb0aXZjyckIvnsFH/tsGMfv+0IDS3+soHJgUAg799x9zS5maHxPSZVfgZymN!p9yx2SioumkSCFbKvHcPZX6rC+Ko1OSFraepPrlSpqw= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3118 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:637 On 4/24/2011 12:34 AM, Merciadri Luca wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Patricia Shanahan writes: > >> On 4/23/2011 10:08 AM, Merciadri Luca wrote: >>> -----BEGIN PGP SIGNED MESSAGE----- >>> Hash: SHA1 >>> >>> Hi, >>> >>> In some of my code files, I've got the following catch block >>> >>> == >>> catch (IOException cantOpenFile) >>> { >>> System.err.println("Can't open " + inputFilename + "."); >>> System.exit(1); >>> } >>> == >>> more than once, but at different places in the file. Is it possible to >>> define this somewhere so that I can directly catch the exception with >>> the two given commands? (Just as one would define his own exception.) >> >> 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? If I understand the question correctly, the difference is which exception the high level method sees. I generally design and document the exceptions a method throws along with designing and documenting its arguments and return. Sometimes it may make sense to let something like IOException filter up. In other cases it is better to throw an exception that adds information the method has, and wraps the IOException as its cause. Patricia