1

Option A:

public void method() {
  try {
    // some operation that throws FirstException
  } catch (FirstException ex) {
    throw new RuntimeException(ex);
  }
  try {
    // some operation that throws SecondException
  } catch (SecondException ex) {
    throw new RuntimeException(ex);
  }
}

Option B:

public void method() {
  try {
    // some operation that throws FirstException
    // some operation that throws SecondException
  } catch (FirstException ex) {
    throw new RuntimeException(ex);
  } catch (SecondException ex) {
    throw new RuntimeException(ex);
  }
}

Which one is better and why?

yegor256
  • 102,010
  • 123
  • 446
  • 597

4 Answers4

2

If your question is about performance then No, there is no difference in both the options. They both have the same processing overhead, the same amount of compilation time (more or less). The question is more about functionality and readability.

Ofcourse option B is more readable.

But if you were not throwing a RuntimeException in your catch blocks then i would have suggested using option A because in option A the second operation will execute even if the first exception is thrown.

After an exception is thrown from a try block the execution control will never return to the same try block.

But in option A since both operations are in a separate try block, unlike option B, so there will be no such problem. After encountering an exception in first operation and handling it you can continue to second operation if you code permits in option A.

However if you are adamant about throwing RuntimeException from catch blocks then like i said there is no difference in either option except for readability.

Surender Thakran
  • 3,958
  • 11
  • 47
  • 81
  • "in option A the second operation will execute even if the first exception is thrown" - are you sure?? – yegor256 May 22 '12 at 15:43
  • 1
    @yegor256: plz read the whole answer i also mentioned somewhere that it will happen if you don't throw `RuntimeException` **and** `if you code permits` by which i mean is that from the catch block of the first exception you should choose to return to the normal program execution by handling it in the code instead of outside it (ex. printing an error message on the console and terminating it will not return you to normal execution etc.) – Surender Thakran May 22 '12 at 15:50
0

If you were not throwing the RuntimeException(ex) then in the first option the second try would have executed as well.

In the second option If the first line in the try had thrown the FirstException it would not have executed any other line in the try block..

JHS
  • 7,761
  • 2
  • 29
  • 53
0

In option A you will be able to catch both Exceptions. In option B you will catch SecondException if and only if the FirstException is not thrown

If FirstException is always thrown then you have unreachable statements (code for SecondException). This will not generate a compiler error

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
0

Luiggi is right. B is more easier to read. But David said something very important: since we are not differentiating exception handling your can just do:

public void method() {
  try {
    // some operation that throws FirstException
    // some operation that throws SecondException
  } catch (Throwable ex) {
    throw new RuntimeException(ex);
  }
}

given that we don't want to deal with future new exception differently.

Zecas
  • 647
  • 4
  • 23
  • 5
    This is poor error handling. Catching Throwable is very risky as it will frequently mask coding errors. Catching a specific kind of exception makes your intention more obvious and runs a smaller risk of catching an unexpected exception (like OutOfMemoryError.) – Spina May 22 '12 at 14:38
  • Absolutely agree Spina, but in this case we are not masking it: we are just re-throwing it. Also, can always replace Throwable by Exception if both exception extend Exception. – Zecas May 22 '12 at 14:41