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?
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.");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With