Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try Catch blocs. question from jls

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What meaning of this:
It is a compile-time error if a type variable is used in the denotation of the type of an exception parameter.
from Jls 7: 14.20. The try statement
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm , doesn't make allot of sense to me . But taking into account the word used it makes me think of a statement like this :

throw new Exception(Exception e) . I m probably wrong trough ...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took that to mean that you can't use generics. But Java doesn't let you create a generic subclass of exception in the first place so it wouldn't make sense to catch it.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:I took that to mean that you can't use generics. But Java doesn't let you create a generic subclass of exception in the first place so it wouldn't make sense to catch it.

An example of the above statement:


The reason why it's not allowed, has something to do with generics which are not covered in the OCAJP7 exam. So just for completeness: generics are only present at compile time, but the generic type parameter is erased at runtime (aka "type erasure"). So let's assume the following code:

MyException<Integer> and MyException<String> are at runtime erased to the same type (MyException), so there's no way the JVM can distinguish between both exception instances and thus can't know which catch block to execute.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergej Smoljanov wrote:What meaning of this:
It is a compile-time error if a type variable is used in the denotation of the type of an exception parameter.

Maybe the exception parameter (used in the catch block/clause) has to be Throwable or a subclass. Any other class will result in a compiler error.



Just my guess.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Maybe the exception parameter (used in the catch block/clause) has to be Throwable or a subclass. Any other class will result in a compiler error.

The previous sentence is the JLS covers that
Each class type used in the denotation of the type of an exception parameter must be the class Throwable or a subclass of Throwable, or a compile-time error occurs.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Roel De Nijs wrote:Maybe the exception parameter (used in the catch block/clause) has to be Throwable or a subclass. Any other class will result in a compiler error.

The previous sentence is the JLS covers that
Each class type used in the denotation of the type of an exception parameter must be the class Throwable or a subclass of Throwable, or a compile-time error occurs.

Maybe the JLS writers just wanted to make sure it's really obvious that using Object or Integer will result in a compiler error
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:I took that to mean that you can't use generics. But Java doesn't let you create a generic subclass of exception in the first place so it wouldn't make sense to catch it.

I had a look at the JLS of Java 5.0 / 6.0. In the description of the try-block you'll find the following quote:

JLS wrote:A try statement may have catch clauses (also called exception handlers). A catch clause must have exactly one parameter (which is called an exception parameter); the declared type of the exception parameter must be the class Throwable or a subclass (not just a subtype) of Throwable, or a compile-time error occurs.In particular, it is a compile-time error if the declared type of the exception parameter is a type variable (ยง4.4). The scope of the parameter variable is the Block of the catch clause.


So it's definitely about not using generics in the catch clause.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exact same paragraph apart from the highlighted part appears in the 2nd edition of the JLS (pre generics), so that would support it being related to generics as well.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:The exact same paragraph apart from the highlighted part appears in the 2nd edition of the JLS (pre generics)

I briefly searched for it on Oracle's website, but couldn't find it Luckily this PDF document popped up and here's the exact same paragraph (without the highlighted part).

JLS 2nd edition wrote:A try statement may have catch clauses (also called exception handlers). A catch clause must have exactly one parameter (which is called an exception parameter); the declared type of the exception parameter must be the class Throwable or a subclass of Throwable , or a compile-time error occurs. The scope of the parameter variable is the Block of the catch clause.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic