1.

What are the differences between error and exception in Java?

Answer»

An error occurs when a user performs an unauthorised action that causes the code to behave abnormally. Errors in programming are frequently UNNOTICED until the code is compiled or run. Some of the errors make it impossible to compile or run a program. As a result, errors should be removed before COMPILING and running a program. Out of memory and system crash are two examples of errors.

In Java, an exception is an unwelcome or unexpected occurrence that occurs during the program execution, or during run time, and disturbs the usual flow of the program's instructions. Exceptions are circumstances that arise during the program execution and may result in its termination. However, try, catch, and throw keywords can be USED to recover them.

Unchecked exceptions (exceptions which are not checked at compile time), such as ArrayIndexOutOfBoundException, are KNOWN to the compiler at runtime, whereas checked exceptions (exceptions which are checked at compile time), such as IOException, are known to the compiler at compile time.

ErrorsExceptions
It is not feasible to recover from an error during compilation and execution.Exceptions can be RECOVERED by utilising a try-catch block or by throw keyword.
In Java, all errors are of the unchecked type.There are exceptions for both checked and unchecked types.
The environment in which the code is running is the most common cause of errors.Exceptions are generated by the code itself.

Errors can arise both at compile and run time.

Compile Time: eg Syntax Error

Run Time: Logical Error.

All exceptions occur at runtime.
Errors are defined in the java.lang.Error package.Exceptions are defined in the java.lang.Exception package
Examples : java.lang.StackOverflowError, java.lang.OutOfMemoryErrorExamples : Checked Exceptions : SQLException, IOException Unchecked Exceptions : ArrayIndexOutOfBoundException, NullPointerException, ArithmeticException.


Discussion

No Comment Found