Exception class. Similarly, classes that inherit from the Error class should end with the string "Error".Exception class. Similarly, classes that inherit from the Error class should end with the string "Error".objectAt(int n) nth position in the list.firstObject indexOf(Object n) Object and returns its position in the list.objectAt firstObject indexOf But what type of exception should each method throw? Should it be an exception provided with the Java development environment? Or should you roll your own?
Your linked list class can throw multiple exceptions, and it would be convenient to be able to catch all exceptions thrown by the linked list with one exception handler. Also, if you plan to distribute your linked list in a package, all related code should be packaged together. Thus for the linked list, you should roll your own exception class hierarchy.
The following diagram illustrates one possible exception class hierarchy for your linked list: 
LinkedListException is the parent class of all the possible exceptions that can be thrown by the linked list class. Users of your linked list class can write a single exception handler to handle all linked list exceptions with a catch statement like this:
catch (LinkedListException ex) {. . . }LinkedListException. The diagram above does not indicate the superclass of the LinkedListException class. As you know, Java exceptions must be Throwable objects (they must be instances of Throwable or a subclass of Throwable). So, your temptation might be to make LinkedListException a subclass of Throwable. However, the java.lang package provides two Throwable subclasses that further divide the type of problems that can occur within a Java program: Errors and Exceptions. Most of the applets and applications that you write will throw objects that are Exceptions. (Errors are reserved for serious hard errors that occur deep in the system.)
Theoretically, any Exception subclass could be used as the parent class of LinkedListException. However, a quick perusal of those classes show that they are either too specialized or completely unrelated to LinkedListException to be appropriate. Thus, the parent class of LinkedListException should be Exception.
Because runtime exceptions don't have to be specified in the throws clause of a method, many packages developers ask: "Isn't it just easier if I make all of my exception inherit from RuntimeException?" The answer to this question is covered in detail on Runtime Exceptions--The Controversy. The bottom line is that you shouldn't subclass RuntimeException unless your class really is a runtime exception! For most of you, this means "No, your exceptions shouldn't inherit from RuntimeException."
Making the exception class
class MyException extends Exception
{
String message;
//----------------------------------------------
// Default constructor - initializes instance variable to unknown
public MyException()
{
super(); // call superclass constructor
message = "No message,just for name";
}
//-----------------------------------------------
// Constructor receives some kind of message that is saved in an instance variable.
public MyException(String errMsg)
{
super(err); // call super class constructor
message= errMsg; // save message
}
//------------------------------------------------
// public method, callable by exception catcher. It returns the error message.
public String getMessage()
{
return message;
}
}
Throwing the above exception:
Consider the following method, which takes inputString and if its not "Vaani", it throws exception.
private void checkWord(String s) throws SpellException
{
if ("Vaani".equalsIgnoreCase(s))
System.out.println("OK");
else
throw new SpellException("Word is not Vaani");
}
try
{
checkWord(); // this method throws SpellException
}
catch (MyException ex) // but it is caught here
{
System.out.println("Spell exception was: " + ex.getMessage());
}