- Error: An Error “indicates serious problems that a reasonable application should not try to catch.”
- Exceptions : An Exception “indicates conditions that a reasonable application might want to catch.”
- There are three types of Errors in Java.
- 1.Compile-time errors.
- 2. Run time errors.
- 3.Logical errors.
Compile-time errors.
- Errors that occur during compiling the program.
- Example:
- missing a semicolon at the end of a statement
- missing braces
- class not found
- These errors will be detected by java compiler and displays the error.
- Full Example:
class CompileTimeDemo
{
public static void main(String args[])
{
int i=10;
//Missing semicolon at the end of statement
System.out.println(i)
}
}
Run time errors.
- Run Time errors are occurs during execution of program.
- Java compiler will not detect Run Time errors.
- These are detected by the JVM while the program is running.
- Full Example:
{
public static void main(String args[])
{
int arr[] = new int[4];
arr[5] = 100;
System.out.println("Success");
}
}
Logical errors.
- Logical error means no errors during compile & run time but not proper output.
- It will not be detected by a compiler nor by the JVM.
- Errors may be generate due to wrong idea or concept used by a programmer while coding.
- Full Example:
class LogicalErrorDemo
{
public static void main(String args[])
{
int i = Integer.parseInt("Hello");
System.out.println(i);
}
}
Exception:
- Exception is a runtime error which can be handled or prevented.
- It appears during the execution of the program.
- There two types of exceptions:
- 1. Checked Exception (also called Compile time exception)
- The exceptions can be detected by java compiler while compiling are called checked exceptions.
- 2. Unchecked Exception (also called Run time exception)
- The exception detected by JVM during runtime is called unchecked exceptions.
- Full Example:
public static void main(String args[])
{
try
{
int arr[] = new int[4];
arr[5] = 100;
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Success");
}
}
- Both Errors and Exceptions are the subclasses of java.lang.Throwable class.
- Throwable is a class in java.lang package.
- It is represents all errors and exceptions.
- It surely cause termination of the program abnormally.
Error Vs. 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 the program is running.
- The program itself is responsible for causing exceptions.
- Errors occur at runtime and not known to the compiler.
- All exceptions occur at runtime but checked exceptions are known to the 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.