• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Inner static vs non static exception class in Java

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did notice a weird behaviour about the static and non-static inner exception classes.

For example the below code will not compile:

   

But the below code will compile:



Why is this behaving this way?

One thing is for sure that we cannot have Generic exceptions due to type erasure but then the above is not a Generic exception class but it is inside the Generic class. But if it is static it is allowed but if it is non-static it is not allowed?

Follow up question, is generic exception not allowed just because of the type erasure feature of Generic like it mentioned here:
https://www.mscharhag.com/java/java-exceptions-and-generic-types
Or there is some other reason to it?
 
Saloon Keeper
Posts: 14490
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner classes of generic classes are also generic themselves, regardless of whether they declare additional type parameters or not.

An inner class really isn't much different from a top level class that has a reference to the enclosing class.

Your first code snippet is roughly equivalent to:

Instances of inner classes don't live in isolation but are tightly coupled to the enclosing class. If you were to catch an exception, you would have to declare it like this:

The article you linked to already explains why this doesn't work.
 
Stephan van Hulst
Saloon Keeper
Posts: 14490
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Amar wrote:Follow up question, is generic exception not allowed just because of the type erasure [...] Or there is some other reason to it?


I can only speculate, but I imagine that allowing exceptions to be generic would make it significantly harder to write a compiler that handled generic catch clauses gracefully. The designers probably thought it wasn't worth the trouble, and I don't blame them.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic