Exceptions

Now let us dwell onto Exceptions which indicates conditions that a reasonable application might want to catch. Exceptions are the conditions that occur at runtime and may cause the termination of the program. But they are recoverable using try, catch and throw keywords. Exceptions are divided into two categories:

  1. Checked exceptions
  2. Unchecked exceptions

Checked exceptions like IOException known to the compiler at compile time while unchecked exceptions like ArrayIndexOutOfBoundException known to the compiler at runtime. It is mostly caused by the program written by the programmer.

Example Exception


\begin{lstlisting}
// Java program illustrating exception thrown
// by Aritmetic...
...// using printStackTrace() method
e.printStackTrace();
}
}
}
\end{lstlisting}

The above program cause the following output


\begin{lstlisting}
java.lang.ArithmeticException: / by zero ....
\end{lstlisting}