Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

IOException and try catach IOException in one method  RSS feed

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Within the method below, throws an IOException and two try catch statements within the same method. Would the two try catch statements activate even with the throws declaration of IOException?

 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Would the two try catch statements activate even...."
- Not sure what do you mean by activate here.

The throws clause you add at the method level is for the entire method. So at multiple places you can actually have catch saying "catch (IOException exception) {....}

Just a thought, if you are having multiple try catch blocks in your method, mostly you are doing some important operations inside the try{...} blocks, can you not re-factor them and keep it as separate methods or separate classes (since they are related to IO operations). But that's a design decision I would leave it to you.

Also looking at your code, you might want to refactor and may be try and move it into a single try{....} block.
 
Tony Docherty
Bartender
Posts: 3203
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When an exception is thrown within one of the try blocks the associated catch statements are checked to see if any of them can handle the type of exception that has been thrown and if so that catch block is executed and if not the exception is thrown to the calling method. In this particular case an IOException thrown within either of the try blocks will be caught and handled by their respective catch statements. If any code outside of those try blocks throws an IOException it will be thrown to the calling method to handle hence the method declares it can throw an IOException.

BTW the design is it is poor and the implementation is flawed.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
BTW the design is it is poor and the implementation is flawed.


The design of my code? If so can you explain please so I can learn.....
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that "fileWriter" and "writer" are declared outside that method. In my opinion they should be local variables, since the file is completely written within the method and there's no need for code outside it to know anything about that.

And second, line 5 does nothing useful -- it declares a variable whose scope is that line only. Did you think that "new File(xxx)" create a new file in the file system? It doesn't -- read its documentation. And even if it did, there's no reason to believe that it would create a file when line 3 failed to do so. And even if it did create a file, that wouldn't be helpful because you still wouldn't have a FileWriter using that file.

Your other catch-block is useless too. It's impossible for supplierList to be an empty list at that point, because line 10 adds something to it. And anyway that's unlikely to be a reasonable response to an IOException thrown in the catch-block -- any such IOException isn't going to be because there was no data to write.

In short all of your attempts to handle errors are misguided. You'd be better off to just let IOException be thrown if it happens, rather than trying to handle it in this method.

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It appears that "fileWriter" and "writer" are declared outside that method. In my opinion they should be local variables, since the file is completely written within the method and there's no need for code outside it to know anything about that.

And second, line 5 does nothing useful -- it declares a variable whose scope is that line only. Did you think that "new File(xxx)" create a new file in the file system? It doesn't -- read its documentation. And even if it did, there's no reason to believe that it would create a file when line 3 failed to do so. And even if it did create a file, that wouldn't be helpful because you still wouldn't have a FileWriter using that file.

Your other catch-block is useless too. It's impossible for supplierList to be an empty list at that point, because line 10 adds something to it. And anyway that's unlikely to be a reasonable response to an IOException thrown in the catch-block -- any such IOException isn't going to be because there was no data to write.

In short all of your attempts to handle errors are misguided. You'd be better off to just let IOException be thrown if it happens, rather than trying to handle it in this method.



Thank You
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!