Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing an UnsupportedOperationException

So one of the method descriptions goes as follows:

public BasicLinkedList addToFront(T data) This operation is invalid for a sorted list. An UnsupportedOperationException will be generated using the message "Invalid operation for sorted list."

My code goes something like this:

public BasicLinkedList<T> addToFront(T data) {
    try {
        throw new UnsupportedOperationException("Invalid operation for sorted list.");
    } catch (java.lang.UnsupportedOperationException e) {
        System.out.println("Invalid operation for sorted list.");
    }
    return this;
}

Is this the right way of doing this? I just printed out the message using println() but is there a different way to generate the message?

like image 724
CoderNinja Avatar asked Mar 11 '13 00:03

CoderNinja


1 Answers

You don't want to catch the exception in your method - the point is to let callers know that the operation is not supported:

public BasicLinkedList<T> addToFront(T data) {
    throw new UnsupportedOperationException("Invalid operation for sorted list.");
}
like image 116
Jason Avatar answered Sep 19 '22 16:09

Jason