• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Try Catch blocs. question from jls

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic