0

Why for Eclipse this is to be managed with a try-with arm?

BufferedReader reader = null;
try {
  if (condition) {
    try {
      reader = method1();
    } catch (Exception e) {
      ...
    }
  }
  if (reader == null) {
    reader = method2();
  }

  do things ...
} catch(Exception e) {
  ...
} finally {
  if (reader != null) {
    reader.close();
  }
}

There is a better way to handle this case? or just a junk warning from eclipse?

This case is not valid:

try (BufferedReader reader = null) {
  if (condition) {
    reader = method1();
  } else {
    reader = method2();
  }

  do things ...
}
6
  • 1
    What do you mean by this case is not valid? Jan 22, 2014 at 11:44
  • 1
    You know that you're not catching the possible exception right?
    – Maroun
    Jan 22, 2014 at 11:45
  • What is the actual warning message you receive from eclipse and where it is placed?
    – Pshemo
    Jan 22, 2014 at 11:46
  • @ᴍaroun, i'm catching them, adding to my sample code. Thks
    – Seby
    Jan 22, 2014 at 11:47
  • @Pshemo Resource 'reader' should be managed by try-with-resource
    – Seby
    Jan 22, 2014 at 11:47

5 Answers 5

1

Try:

try (BufferedReader reader = createBufferedReader(condition)) {
  do things ...
}

private BufferedReader createBufferedReader(boolean condition){
  if (condition) {
    return method1();
  } else {
    return method2();
  }
}
8
  • This will be valid, but i'm not creating a new method to avoid that warning, seems to me killing an ant with a gun.
    – Seby
    Jan 22, 2014 at 11:51
  • 1
    Not to avoid the warning - to write less error-prone and more concise code. And that's why you get the warning.
    – Puce
    Jan 22, 2014 at 11:53
  • Not to be polemic, but this way when i look at the first line of code i don't know what createBufferedReader is doing, than i have to look at it than come back to first method. This is surely not more concise code, on the contrary. (obviously having many code in do things ... line).
    – Seby
    Jan 22, 2014 at 11:57
  • I recommend you to learn about "Clear Code", also when working in a team.
    – Seby
    Jan 22, 2014 at 11:59
  • Another source: Martin Fowler, Refactoring, Exctract Method: refactoring.com/catalog/extractMethod.html
    – Puce
    Jan 22, 2014 at 12:02
1

As Java Language Specification states in jls-14.20.3

A resource declared in a ResourceSpecification is implicitly declared final (§4.12.4) if it is not explicitly declared final.

So you can't change it in your try block. If you want to be able to change it use standard try-catch-finally block. Other option would be deciding about correct resource before useing it with try-with-resources.

2
  • Using standar try-catch block is throwing the warning.
    – Seby
    Jan 22, 2014 at 12:10
  • @user509375 I can't reproduce your warning.
    – Pshemo
    Jan 22, 2014 at 12:13
1

You don't necessarily need a Callable or lambda expression as in Seby's answer.

Given that the problem is simple enough you can simply use a ternary operator, which works in all versions of java.

final String s = "abc";

try (BufferedReader reader = (condition) ? method1() : method2();) {

  do things ...

} catch (Exception e) {
  ...
}
0

you can use a function which has a return type of BufferedReader in which you can use your conditional call, this function can be invoked from main only if some specific condigion that you are looking for is met.

private BufferedReader createReader(!XYZ){
if(a>b)
return abc();
else
return def();
}

public static void main(String[] args){
createReader(!XYZ);
}
0

Best way imho, clear and concise :

final String s = "abc";

try (BufferedReader reader = new Callable<BufferedReader>() {
  @Override
  public BufferedReader call() throws Exception {
    if (condition) {
      return method1();
    } 
    return method2();

    // different scope, s needs final keyword
  }
}.call()) {

  do things ...

} catch (Exception e) {
  ...
}

Also easier in java 8 :

String s = "abc";

try (BufferedReader reader = () -> {
  if (condition) {
    return method1();
  } 
  return method2();

  // same scope, s does not need final keyword
}) {

  do things ...

} catch (Exception e) {
  ...
}
4
  • You need to invoke call() after anonymous Callable class.
    – Pshemo
    Jan 22, 2014 at 13:02
  • Anyway IMHO using Callable here is misusing this interface. I would prefer Puce's approach with additional private method. This will prevent you from having to create final references so you can pass them to Callable anonymous class. Also it is better divide code to short parts that can be maintained separately (like methods).
    – Pshemo
    Jan 22, 2014 at 13:30
  • @Pshemo IMHO separating code in short parts make code less readable, can be ok in small classes or little projects, but experience tells me that in large teams and big projects with hundred classes this approach is far less preferable. Instant readability is better in any way to fast understand blocks of code.
    – Seby
    Jan 22, 2014 at 13:35
  • @Pshemo Also, in java 8 the "final" problem gone away.
    – Seby
    Jan 22, 2014 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.