In my program, I have created doubly linked list. For inserting and deleting I have defined methods Insert(int element,int pos) and Delete (int pos). Those two methods throw exception. In the same class I have defined Exception LinkedListException() which returns new custom exception class HW2Exception. From my main I created the object for insertion and deletion in try-catch block.
So in the below example the 4th and the 5th insertions dont link to the list.
In my previous question I got many suggestion to make it with some methods
- Is there any possibility to continue to program by changing the HW2Exception class?
- If I dont want to halt the insertion or deletion what should I do?
public static void main(String[] args) {
LinkedList myList = new LinkedList();
try {
myList.Insert(1, 0);
myList.Insert(2, 0);
myList.Insert(5, 54); //false entry
myList.Insert(2, 0);
myList.Insert(0, 0);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
myList.Output();
public int Delete(int pos) throws Exception {
int element; // variable for holding the deleted node's element value
if (head == null) { //
System.out.print("In Delete method->");
throw LinkedListException();
public void Insert(int newElement, int pos) throws Exception {
DoubleLinkNode newNode = new DoubleLinkNode(newElement);
if (head == null && pos == 0) {
head = newNode;
} else if (pos == 0 && head != null) {
newNode.right = head;
head.left = newNode;
head = newNode;
.....
...
..
..
} else {
System.out.print("In Insert method->");
throw LinkedListException();
}
So also in the same class I have defined
public Exception LinkedListException() {
//if we dont use the custom exception class
// throw new UnsupportedOperationException("False entry for pos variable ");
return new HW2Exception("False entry for pos variable ");
}
this is my custom exception class
public class HW2Exception extends Exception{
// parameterized constructor that accepts only detail message
public HW2Exception(String message){
// calling parent Exception class constructor
super(message);
}
}
In my previous question I got many suggestion to make it with some methods
- Is there any possibility to continue to program by changing the HW2Exception class?
- If I dont want to halt the insertion or deletion what should I do?