Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Can not define a parametrized exception class.

Thread view: 
lichuan.nj@gmail.com - 16 Nov 2007 09:42 GMT
Defining

/**
* Puzzled.
*
* @author cli
*
* @param <T>
*/
public class ClassException <T> extends Throwable
{
    public ClassException(String msg) {
        super(msg);
    }
}

gives the compiling message "The generic class ClassException<T> may
not subclass java.lang.Throwable".

Can not we define an parametrized exception class?...

Thanks a lot. :)
Piotr Kobzda - 16 Nov 2007 12:14 GMT
> Can not we define an parametrized exception class?...

<http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ007>
<http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#FAQ302>

piotr
lichuan.nj@gmail.com - 16 Nov 2007 12:32 GMT
> lichuan...@gmail.com wrote:
> > Can not we define an parametrized exception class?...
[quoted text clipped - 3 lines]
>
> piotr

Thanks, I understood now as: due to the type erasure, T in a generics
declaration actually becomes the Object type in bytecodes at runtime.
That also answers why we can create a raw type without
paramatrization. Anything is an object ..... Generics is a compiling
time magic for type safy...
Lew - 16 Nov 2007 15:32 GMT
>> lichuan...@gmail.com wrote:
>>> Can not we define an parametrized exception class?...
[quoted text clipped - 8 lines]
> paramatrization. Anything is an object ..... Generics is a compiling
> time magic for type safy...

Better still to quote the law,
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.2>
> It is a compile-time error if a generic class is a direct or indirect subclass of Throwable.

OTOH, it's perfectly fine for Exceptions to be type parameters in a generic
declaration.

public class Handler <T extends Exception> ...

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.