Errors Vs. Exceptions

In java, both Errors and Exceptions are the subclasses of java.lang.Throwable class. Error refers to an illegal operation performed by the user which results in the abnormal working of the program. Programming errors often remain undetected until the program is compiled or executed. Some of the errors inhibit the program from getting compiled or executed. Thus errors should be removed before compiling and executing. It is of three types:

  1. Compile-time
  2. Run-time
  3. Logical

Whereas exceptions in java refer to an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions.

Figure 5.1: Exceptions in Java
Image Exception-in-java

Now let us discuss various types of errors in order to get a better understanding over arrays. As discussed in the header an error indicates serious problems that a reasonable application should not try to catch. Errors are conditions that cannot get recovered by any handling techniques. It surely causes termination of the program abnormally. Errors belong to unchecked type and mostly occur at runtime. Some of the examples of errors are Out of memory errors or System crash errors.

Example 1: Compile-time Error


\begin{lstlisting}
// Java Program to Illustrate Error
// Stack overflow error v...
...
// custom integer as an argument
StackOverflow.test(5);
}
}
\end{lstlisting}

Example 2: Compile-time Error


\begin{lstlisting}
// Java Program to Illustrate Run-time Errors
\par
// Main cl...
...else
System.out.println(c
+ '' is the largest Number'');
}
}
\end{lstlisting}

Output


\begin{lstlisting}
8 is the smallest Number
\end{lstlisting}



Subsections