java - Why is throwing a checked exception type allowed in this case? -


I saw from accident that it was compiled by the throw statement (removed from some more complex code):

/ P>

  void foo () {try {} catches (throttle t) {throw t; }}  

For a brief but happy moment I thought that the exceptions had already been decided to die already, but it still gets excited:

  zero foo () {try {} catches (throbbles) {throbable t 1 = t; Throw T1; }}  

Try the block should not be empty; It appears that the code can be in such a long time, unless the code throws that check exception. It sounds reasonable, but my question is, which rules in the language specification describes this behavior? As far as I can see, it explicitly restricts it, because t is an exception to the type of expression, and it is not announced to be caught or thrown. (?)

I think the word that has been used is a mistake in JLS & Mdash; The text which should be updated with Java SE7, and was not.

JLS is a bit of text that describes the behavior that is desired:

A Throw statement whose throwing expression is a hold < The last exception can be the final or effective form of the code / segment> Section C. Extra exception class E iff:

  • E is an exception to class try block Try statement that declares; And
  • The catchable exceptions of EC are compatible with any of the sections; And
  • The e assignment is not compatible with any attractive exception classes of hold , which is declared on left side of C, in the same try Statement

The first bullet point is relevant; Because hold -clause parameter t is effectively the last (which means that it has never been assigned or increased or decreased; see), Throw can only throw block the block.

But as you say, the investigation of compilation-time §14.18 does not make any allowance for this. § 11.2.2 does not decide whether permission is or not; Rather, it can be thrown but the results of various restrictions are considered to be analyzed. (This analysis feeds back-to-the-standard parts of the features & mdash; §14.18 automatically uses it in its second bullet point; but §14.18 can say that "this is a compile-time error if it adds an exception Because this will be the circular.)

So I think §14.18 need to be adjusted to accommodate the intentions of § 11.2.2.

Good search!


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -