Exceptions

Definition

An exception occurs when there is a problem executing your program. There are many different types of exceptions for different problems in your code.

Types of Exceptions

Exceptions that can occur while executing your code are as follows:

Types Possible reason for exception
ArithmeticException Cannot divide by zero
ArrayIndexOutOfBoundsException When a negative index or an index that is too large is used for an array.
IllegalArgumentException If a negative argument is used in a square root function.

Examples

When an ArithmeticException will occur:

int i = 0;
System.out.print(300/i); // cannot divide by zero!

When an ArrayIndexOutOfBounds will occur:

for(int i = 0; i <= x.length; i++){ // i will equal x.length which is one index past the last
    x[i] = i;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License