3

Assume a method getBar which throws a checked exception. It takes an instance of Foo. Foo is never used anywhere else in code and it does not throw exception and its only job is to be a parameter to getBar. Should foo be declared and initialized inside or outside a try ? Extending the question - should code inside try be minimal (as my second option) or wrap a block of similar related lines of code (as my first option) ?

try {
   Foo foo = new Foo();
   var result = SomeConnection.getBar(foo);
   return result == foo.RESULT;
} catch ( .. ) {...}

OR

Foo foo = new Foo();
try {
   SomeConnection.getBar(foo);
   return result == foo.RESULT;
} catch ( .. ) {...}
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132

4 Answers4

3

With your code, there are no performance issues with minimizing the amount of code inside a try block. The (minimal) performance impact is all in entering and exiting the try block itself.

EDIT: If Foo were something that implements java.lang.AutoCloseable (including anything that implements java.io.Closeable), you should use a "try-with-resources" statement:

try (Foo foo = new foo()) {
    var result = SomeConnection.getBar(foo);
    return result == foo.RESULT;
} catch (...) {...}

Note that this Java 7 language construct clearly makes foo local to the try block.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Iterating my core concern - should code inside try be minimal (as my second option) or wrap a block of similar related lines of code (as my first option) ? – JavaDeveloper Nov 13 '13 at 02:48
  • @JavaDeveloper - That's strictly a matter of style, as long as scoping is not an issue (that is, you don't need the variable outside the `try` block--that includes the `catch` and/or `finally` blocks). There are no performance issues to consider. Personally, I would probably put the declaration inside to emphasize the local scope of the variable. – Ted Hopp Nov 13 '13 at 02:51
1

In addition to Ted's answer, you'd put the foo inside the try block if it had had constructor logic that could throw an exception. Other than that, in terms of performance there is no difference. I'd personally go with your first version and keep foo in the scope that I'm using it in. In terms of best practice, I don't think having "minimal" code inside your try/catch makes much of a difference. On the other hand having too many try/catch blocks (or even worse, having them nested) is a bad practice.

Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • repeating to create an event for you - Iterating my core concern - should code inside try be minimal (as my second option) or wrap a block of similar related lines of code (as my first option) ? – JavaDeveloper Nov 13 '13 at 02:48
  • @JavaDeveloper Check my edit, I've added some clarifications, I hope it helps. – Dimitar Dimitrov Nov 13 '13 at 02:52
0

Wrapping extra stuff in a try/catch isn't necessarily bad, but in some languages, what is thrown is a different exception type (e.g. ExceptionInvalidParameter, ExceptionUnknown, ...) and you can have multiple catch blocks for each type. I like to wrap tons of stuff in try/catch and have a finally statement to handle unexpected exceptions.

0

Your program will always execute Foo foo = new Foo() so you can't gain performance there, what you could do is minimize your code:

SomeConnection.getBar(new Foo())

Assuming foo.RESULT is a constant it could be an static constant Foo.RESULT.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73