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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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_right

    
    

    Output:



    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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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_right

    
    

    Output:

    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.OutOfMemoryError
    Examples :
    Checked Exceptions : SQLException, IOException
    Unchecked Exceptions : ArrayIndexOutOfBoundException, NullPointerException, ArithmeticException.


    My Personal Notes arrow_drop_up

    Geeksforgeeks Intern

    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.