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 recursionclassStackOverflow {publicstaticvoidtest(inti){// No correct as base condition leads to// non-stop recursion.if(i ==0)return;else{test(i++);}}}publicclassErrorEg {publicstaticvoidmain(String[] args){// eg of StackOverflowErrorStackOverflow.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 classpublicclassExceptionEg {publicstaticvoidmain(String[] args){inta =5, b =0;// Attempting to divide by zerotry{intc = 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.
Recommended Posts:
- Exceptions in Java
- Chained Exceptions in Java
- Rounding off errors in Java
- Checked vs Unchecked Exceptions in Java
- Built-in Exceptions in Java with examples
- Using throw, catch and instanceof to handle Exceptions in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



