+ 4
Java exceptions are thrown when an error occours while program execution. By catching them, you can be notified about the (kind) of reason and place of the error occurance.
Their description (which can be set inside the constructor as parameter and got by the getDescription() method) holds further textual information or details about the reason of error.
Typical exceptions:
- NullPointerException:
You wanted something from an object, which is null, like o.toString() while o is null.
- NumberFormatException:
You tried to parse a number from a string, but the string was not in the right format.
- IndexOutOfBoundException:
You under- or overindexed something, like: array[-1] or array[x] where x >= the length of the array.
- IllegalArgumentException:
One of the arguments/parameters passed to the method cannot be handled by the method (forbidden).
For how to handle them check the "try-catch" syntax of java. In a nutshell:
try {
method();
} catch (xException xe) {
... handle exception
}
method() throws xException {
...
}