Sounds like you may want to read up on Java. Check out The Java Tutorials, especially this one about Exceptions.
To put it simply, exceptions are a special kind of object representing an event outside of the normal operation of your code, causing control flow to be subverted. For example, an ArrayIndexOutOfBoundsException
means your code tried to index to a position in an array that did not exist, such as -1.
Because of their association with bugs, exceptions often have a bad connotation to newer programmers. But because Java is Object Oriented, you can extend RuntimeException
to create your own, custom exception types, which is quite useful for debugging and code clarity. To throw a custom exception while your code is executing, you'll have to
- have defined the custom exception,
- detect the exceptional condition, and
throw
the exception.
The easy way to define your custom RuntimeException
is to define a class like:
public EmptyStackException extends RuntimeException {
// customize error messages if necessay
}
Then you'd detect and throw the Exception
like:
if (/** stack is empty */) {
throw new EmptyStackException();
}
These are just the basics. You can also define custom exceptions on the fly. Hope this helps!