1

This question is related to both of these questions - Efficient try / catch block usage? and Dealing with error in data - Idempotent approach.

When I encounter a void while reading a GIS data file I throw an Exception. This Exception is a subclass of java.lang.Exception. The question is of what happens after the void data exception is reached. Post processing would require that I go to another URL where the void filled data file is available and then download that and then proceed to read it in.

From the "efficient-try-catch-block-usage" question it appears doing inside this the catch block is a strict NO. Would it better then instead of throwing an application specific exception it is better to check for a return code or a post condition and proceed to do the post processing ?

1 Answers1

0

It depends on how much of a performance hit you can reasonably accept when throwing the exception. If it only happens once, it might not be a big deal. If it happens every line of the file, it could be a huge problem.

Normally exceptions are thrown when something happens that you can't do anything about, and execution is stopped.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • when a single void is encountered I stop processing that file. I then go to the void filled URL and proceed to download that. You are saying this can be done inside the catch block ? –  May 21 '15 at 04:57
  • I'm not a fan of using catch blocks to control program flow, but yes, you could use it like a hacky `continue` statement. – Robert Harvey May 21 '15 at 05:16
  • Robert Harvey - not a big fan of hacky code :-) –  May 21 '15 at 05:24
  • Still, you could think of each file as a separate "process." In that context, exceptions used in this way are perfectly legitimate. – Robert Harvey May 21 '15 at 13:10
  • you are saying it is fine to have detailed application logic inside catch blocks ? –  May 21 '15 at 13:23
  • You have a function. It reads the lines of a text file and does something with each line. If there's a problem with one of the lines, it throws an exception and stops. Now wrap that function in a loop, so that it can handle more than one file. See? – Robert Harvey May 21 '15 at 13:27
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/23993/discussion-between-gansub-and-robert-harvey). –  May 21 '15 at 13:42