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]

# 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.
# 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...");
}