Monday, February 1, 2016

Generics and Exceptions - Java

Generics:
class ListNode {
    ListNode next;
    ListNode prev;
    E data;}
E: parameterized type

Exceptions:
throws exceptions to indicate fatal problems.

Checked exception must be declared.
public class RememberLast {
public T add(T element) throws NullPointerException { //Not required since NPE is unchecked, but OK
if (element == null) {
throw new NullPointerException("T cannot store null pointers");
}
...
}
}


No comments:

Post a Comment