In C# and in Java (and possibly other languages as well), variables declared in a \"try\" block are not in scope in the corresponding \"catch\" or \"finally\" blocks. For e
The answer, as everyone has pointed out, is pretty much "that's how blocks are defined".
There are some proposals to make the code prettier. See ARM
try (FileReader in = makeReader(), FileWriter out = makeWriter()) {
// code using in and out
} catch(IOException e) {
// ...
}
Closures are supposed to address this as well.
with(FileReader in : makeReader()) with(FileWriter out : makeWriter()) {
// code using in and out
}
UPDATE: ARM is implemented in Java 7. http://download.java.net/jdk7/docs/technotes/guides/language/try-with-resources.html