Zdenek-<br><br>In this case E is a disjunction type, and the variables e1 and e2 are of the disjunction type, which means either of them can be either of the two types at runtime.<br><br>Cheers,<br>Neal<br><br><div class="gmail_quote">
On Sat, Nov 21, 2009 at 2:09 AM, <span dir="ltr"><<a href="mailto:tronicek@fit.cvut.cz">tronicek@fit.cvut.cz</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
<br>
[Neal, the reference to closures-v07a.html on <a href="http://javac.info" target="_blank">javac.info</a> does not work.]<br>
<br>
Using the declaration from the proposal<br>
<div class="im"><br>
public static <T,throws E extends Exception><br>
T withLock(Lock lock, #T() throws E block) throws E {<br>
lock.lock();<br>
try {<br>
return block.invoke();<br>
} finally {<br>
lock.unlock();<br>
}<br>
}<br>
<br>
</div>I understand we can use withLock as follows:<br>
<br>
withLock(lock, #() {<br>
Thread.sleep(1000); // throws InterruptedException<br>
new FileInputStream("x").close(); // throws FileNotFoundException and<br>
IOException<br>
});<br>
<br>
and the compiler infers that E is InterruptedException or IOException and<br>
will force us to catch both of them. This is why we need the throws<br>
keyword.<br>
In accordance with the proposal I use "or" (InterruptedException or<br>
IOException) but more logical seems to be "and", as this is the compile<br>
time checking. The compiler must infer that the method may throw<br>
InterruptedException AND IOException.<br>
[The NumberFormatException used in the proposal is not a good choice<br>
because it is a runtime exception.]<br>
<br>
So far, so good. Now, let's use the throws keyword in a method declaration:<br>
<br>
public <throws E extends Exception> void throwit(boolean b, E e1, E e2)<br>
throws E {<br>
if (b) throw e1;<br>
else throw e2;<br>
}<br>
<br>
and call it:<br>
<br>
throwit(b, new InterruptedException(), new IOException());<br>
<br>
If E is inferred the same way, does it mean that e1 and e2 are of<br>
different types?<br>
Does it make sense to use E as a type (except that it can be thrown from<br>
the method)?<br>
<br>
Zdenek<br>
--<br>
Zdenek Tronicek<br>
FIT CTU in Prague<br>
<br>
<br>
Cituji Paul Benedict <<a href="mailto:pbenedict@apache.org">pbenedict@apache.org</a>>:<br>
<div class="im"><br>
> Neal,<br>
><br>
>> Perhaps another approach would be to just take the hint from the<br>
>> fact that the type parameter's bound is a checked exception type. That is<br>
>> not strictly backward source-compatible, but it might be compatible enough<br>
>> to allow us to simplify the syntax as you suggest.<br>
><br>
> You read my mind! I was hoping you could infer it.<br>
><br>
> Anyway, if we take closures out of the discussion, I imagine the new<br>
> type is usable outside of closures, correct? For example:<br>
><br>
</div>> public <throws E extends Exception> void throwit(E e) throws E {<br>
<div class="im">> throw e;<br>
> }<br>
><br>
> But really, the compiler should know what to do just with this:<br>
><br>
> public <E extends Exception> void throwit(E e) throws E {<br>
> throw e;<br>
> }<br>
><br>
> I might be missing something, but is there any important distinction<br>
> between the two?<br>
><br>
</div>> Paul<br>
><br>
<br>
<br>
</blockquote></div><br>