Generics inner class problem

I am trying to set up an exception class inside a generic class. It gives me error and does not compile. Any help, please.

public class DoublyLinkedList<T> {

/**

* This class extends the JAVA API's Exception class to create NotInListException

* This class is to be used to throw error whenever, a non exiting item is to be removed

* from the list

**/

public class ListEmptyException extends Exception {

public ListEmptyException() {

super("The List is empty");

}

}

public class NotInListException extends Exception {

public NotInListException(){

super("This Item does not exist in List");

}

}

Message was edited by:

catbtds

[756 byte] By [catbtdsa] at [2007-11-27 2:12:36]
# 1
You really need to show the exception. You also need to show the code where the exception is created. And it would be helpful if you would use codetags.~Bill
abillconsla at 2007-7-12 2:07:08 > top of Java-index,Core,Core APIs...
# 2
https://bugs.eclipse.org/bugs/show_bug.cgi?id=174766
prometheuzza at 2007-7-12 2:07:09 > top of Java-index,Core,Core APIs...
# 3
1. ERROR in )public class C extends Exception {}^^^^^^^^^The generic class DoublyLinkedList<T>.ListEmptyException may not subclass java.lang.ThrowableI am using Eclipse and its showing this read mark under Exception.
catbtdsa at 2007-7-12 2:07:09 > top of Java-index,Core,Core APIs...
# 4
It seems to be working if I make the class static but I do not understand why .
catbtdsa at 2007-7-12 2:07:09 > top of Java-index,Core,Core APIs...
# 5

Got me too. Two suggestions though:

1. Try making the top level class NOT generic and see if that has anything to do with it. If you still get the same error, we can proceed.

2. If after 1, above, the error goes away, try posting to the 'GENERICS' forum.

~Bill

PS: go back to the non-static method you got the error with.

abillconsla at 2007-7-12 2:07:09 > top of Java-index,Core,Core APIs...
# 6
?
abillconsla at 2007-7-12 2:07:09 > top of Java-index,Core,Core APIs...
# 7

Your example nicely demonstrates a common pitfall in Java programming:

Somebody wants the lexical scoping of class names that comes with inner classes and forgets to declare the class as "static".

I'm quite sure you wanted to make the exception class "belong" to the List's namespace, but you did not want every instance of it bound to a List instance, right?

By making the exception class non-static, you'd make it generic, too, because it gets the outer class' type parameters injected.

Thus, you'd create a generic Exception, which could not be handled by ordinary (non-generic) catch-clauses.

Hey, just for fun, this is what generic exception handling would look like:

DoublyLinkedList<?> list = new DoublyLinkedList<String>();

try {

list.remove(0);

}

catch(DoublyLinkedList<String>.ListEmptyException e) {

System.out.println("got it!");

}

catch(DoublyLinkedList<Integer>.ListEmptyException e) {

System.out.println("shouldn't have gotten here!");

}

catch(DoublyLinkedList<?>.ListEmptyException e) {

System.out.println("one never knows...");

}

McNeppa at 2007-7-12 2:07:09 > top of Java-index,Core,Core APIs...