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


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

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 Sat, 23 Apr 2011 18:22:41 -0400
Organization albasani.net
Lines 127
Message-ID <iovjf9$ieq$1@news.albasani.net> (permalink)
References <87r58s28w3.fsf@merciadriluca-station.MERCIADRILUCA> <iov6eu$ps5$1@dont-email.me> <871v0smxvr.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 8xcNrJ4BG6O42JZ6pk0W+v4Znz9T9OzNaI96vriPARt1qmQF7mP2oMT0WY1l7WYIaJRT0U90Iup8ytQCEwMb4hMk5bKmKYA61a4lb0yb0RjM0/F2gGcbdJsFW00Cnn1W
NNTP-Posting-Date Sat, 23 Apr 2011 22:22:33 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="c/IKkrBGPOZgfqfTQXnnuM6TH/MwAeiokfd5CpJzuaXjoj1UCUv/LCudqDamehjr0y7MhPHr6CmP4Wmo+oZ90Ug2j1Ax00i1eYg/GTMnNqAClqd53CDaAH8wRvlVSdWV"; 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 <871v0smxvr.fsf@merciadriluca-station.MERCIADRILUCA>
Cancel-Lock sha1:2o9MbyNtls60R2q0TGjXXWuDjBY=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:622

Show key headers only | View raw


On 04/23/2011 06:00 PM, Merciadri Luca wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Eric Sosman<esosman@ieee-dot-org.invalid>  writes:
>
>> On 4/23/2011 1:08 PM, Merciadri Luca wrote:
>>> 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.)
>>
>>      Could you give an example of what you're trying to do?  (The
>> above isn't sufficiently informative.)
>
> Thanks for the answers. Sure I can provide more details. Let us
> consider the following constructor:
>
> ==
>     public FileStream ()
>      {
>          try
>              {
>                  outputFile = new BufferedWriter(new FileWriter("hello.txt"));
>              }
>          catch (IOException cantWriteInFile)
>              {
>                  System.out.println("Impossible to write (in) hello.txt.");
>                  System.exit(1);
>              }
>      }
> ==
> You saw that I had to catch an IOException, as expected when dealing
> with a BufferedWriter.
>
> Now, in a function of the same class of the constructor (class
> FileStream), I need to write in the output file. As a result, I've got
>
> ==
> public void write (String outputText)
> {
>          try
>              {
>                  outputFile.write(outputText);
>                  outputFile.close();
>              }
>          catch (IOException cantWriteInFile)
>              {
>                  System.out.println("Impossible to write (in) hello.txt.");
>                  System.exit(1);
>              }
> }
> ==
>
> You directly notice that the two catch blocks are identical. As a
> result, I'd like to do something like this (in pseudocode):
>
> ==
>    public FileStream ()
>      {
>          try
>              {
>                  outputFile = new BufferedWriter(new FileWriter("hello.txt"));
>              }
>          catch (IOException cantWriteInFile);
>      }
> ==
> then, later in the file:
>
> ==
> public void write (String outputText)
> {
>          try
>              {
>                  outputFile.write(outputText);
>                  outputFile.close();
>              }
>          catch (IOException cantWriteInFile);
> }
> ==
> where IOException cantWriteInFile is defined in the class as
>
> ==
>                  System.out.println("Impossible to write (in) hello.txt.");
>                  System.exit(1);
> ==
>
> Is there a way to do this? It looks terribly bad to have the same
> catch code in two different methods.

Not really, and not at all in the fashion you're requesting.

You do NOT want to put 'System.exit()' in a 'catch' block.  I thought we'd 
made that clear already.  Do you attend to the answers we give you?  I wish 
you would, please.

You especially don't want to call 'System.exit()' from within a constructor. 
Pay attention, now.

Some people try putting a "common" method in the 'catch' block that logs, 
closes and exits, but that's a TERRIBLE practice.  Among other things, it 
defeats the compiler's analysis of "definitely (un)assigned".

The problem is that these are different 'IOException's, that is, they have to 
be caught at wildly different times.

As another respondent indicated in another thread, you can do things like what 
you're asking with aspect-oriented programming (AOP).  Sometimes when you 
think about the answers you've been given you can find answers to questions 
you hadn't considered earlier.  It pays HUGE dividends to ponder each answer, 
googling for unfamiliar terms and supplementing with your own effort at 
research and review of materials proffered.

If you don't revuew the advice, why ask for it?

-- 
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