Introduction
When the JVM finds a run-time error in executing a
program, it throws an exception. This means it terminates the execution and
prints a trace of the contents of the run-time stack. This stack is used
to store local variables and addresses of the next statment to be executed when
a method is invoked. There are several types of exceptions; but they are all
subclasses of Throwable. The two direct subclasses of
Throwable are Error and Exception.
public class NegativeNumber extends Exception
{
public NegativeNumber()
{
super();
}
public NegativeNumber(String s)
{
super(s);
}
}
if( a <0) throw new NegativeNumber(a + " a negative value was encountered");
However, the method containing this would need throws NegativeNumber to immediately follow the heading.