Errors V/s Exceptions In Java
Errors V/s Exceptions In Java
- Error : An Error “indicates serious problems that a reasonable application should not try to catch.”
Both Errors and Exceptions are the subclasses of java.lang.Throwable class. Errors are the conditions which cannot get recovered by any handling techniques. It surely cause 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 error or a System crash error.// Java program illustrating stack overflow error
// by doing infinite recursion
class
StackOverflow {
public
static
void
test(
int
i)
{
// No correct as base condition leads to
// non-stop recursion.
if
(i ==
0
)
return
;
else
{
test(i++);
}
}
}
public
class
ErrorEg {
public
static
void
main(String[] args)
{
// eg of StackOverflowError
StackOverflow.test(
5
);
}
}
chevron_rightfilter_noneOutput:
Exception in thread "main" java.lang.StackOverflowError at StackOverflow.test(ErrorEg.java:7) at StackOverflow.test(ErrorEg.java:7) at StackOverflow.test(ErrorEg.java:7) at StackOverflow.test(ErrorEg.java:7) at StackOverflow.test(ErrorEg.java:7) ...
- Exceptions : An Exception “indicates conditions that a reasonable application might want to catch.”
Exceptions are the conditions that occur at runtime and may cause the termination of program. But they are recoverable using try, catch and throw keywords. Exceptions are divided into two catagories : checked and 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.// Java program illustrating exception thrown
// by AritmeticExcpetion class
public
class
ExceptionEg {
public
static
void
main(String[] args)
{
int
a =
5
, b =
0
;
// Attempting to divide by zero
try
{
int
c = a / b;
}
catch
(ArithmeticException e) {
e.printStackTrace();
}
}
}
chevron_rightfilter_noneOutput:
java.lang.ArithmeticException: / by zero at ExceptionEg.main(ExceptionEg.java:8)
Summary of Differences
Errors Exceptions Recovering from Error is not possible. We can recover from exceptions by either using try-catch block or throwing exceptions back to caller. All errors in java are unchecked type. Exceptions include both checked as well as unchecked type. Errors are mostly caused by the environment in which program is running. Program itself is responsible for causing exceptions. Errors occur at runtime and not known to the compiler. All exceptions occurs at runtime but checked exceptions are known to compiler while unchecked are not. They are defined in java.lang.Error package. They are defined in java.lang.Exception package Examples :
java.lang.StackOverflowError, java.lang.OutOfMemoryErrorExamples :
Checked Exceptions : SQLException, IOException
Unchecked Exceptions : ArrayIndexOutOfBoundException, NullPointerException, ArithmeticException.Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.