Errors V/s Exceptions In Java
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:
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.
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
8 is the smallest Number
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:
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.
Finally now wrappingoff the article by plotting the differences out between them in a tabular format as provided below as follows: Errors can occur at compile time as well as run time. Compile Time: eg Syntax Error Run Time: Logical Error.
Errors Exceptions Recovering from Error is not possible. We can recover from exceptions by either using try-catch block or throwing exceptions back to the Errorsthe The programoccurcaller. 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. All exceptions occurs at runtime but checked exceptions are known to the compiler while unchecked isnot. 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.
Errors can occur at compile time as well as run time. Compile Time: eg Syntax Error
Run Time: Logical Error.
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.