
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Exception and Error in Java
In this article, we will compare the Java Error class and the Java Exception class. Both exceptions and errors are subclasses of the Throwable class. The error indicates a problem that mainly occurs due to the lack of system resources and our application should not catch these types of problems.
It's important to note that the term error in general programming refers to mistakes in code, logic, or unexpected behavior. The Java Error class is not the same as general errors.
What is an Error?
An Error is a subclass of the Throwable class that represents serious problems that indicate issues with the Java Virtual Machine (JVM) or system resources. Java applications should not try to catch or handle them.
The following are examples of the errors in Java ?
- OutOfMemoryError: OutOfMemoryError occurs when JVM runs out of memory.
- StackOverflowError: StackOverflowError occurs when an application tries to use more memory than the maximum size allocated.
- InternalError: An InternalError occurs when there is an unexpected internal problem in the JVM.
Example of Error
The following is an example of an error in Java ?
public class ErrorExample { public static void main(String[] args){ recursiveMethod(10) } public static void recursiveMethod(int i){ while(i!=0){ i=i+1; recursiveMethod(i); } } }
The output of the above Java program is ?
Exception in thread "main" java.lang.StackOverflowError at ErrorExample.ErrorExample(Main.java:42)
What are Exceptions?
Exceptions are the problems that can occur at runtime and compile time. It mainly occurs in the code written by the developers. Exceptions are divided into two categories: checked exceptions and unchecked exceptions.
- Checked Exceptions: Checked exceptions are exceptions that are checked at compile-time. For example, IOException is a checked exception.
- Unchecked Exceptions: Unchecked exceptions are not checked at compile-time and extend RuntimeException. For example, NullPointerException is an unchecked exception.
Example of Exception
The following is an example of an exception in Java ?
public class ExceptionExample { public static void main(String[] args){ int x = 100; int y = 0; int z = x / y; } }
The output of the above Java program is ?
java.lang.ArithmeticException: / by zero at ExceptionExample.main(ExceptionExample.java:7)
Difference Between Errors and Exceptions
The following table shows the difference between errors and exceptions ?
Sr. No. | Key | Error | Exception |
---|---|---|---|
1 | Type | The error class and all its subclasses are classified as an unchecked type | Exceptions are classified as checked and unchecked |
2 | Package | It belongs to java.lang.error | It belongs to java.lang.Exception |
3 | Recoverable/ Irrecoverable | It is irrecoverable | It is recoverable |
4 | Compile-time check | It can't occur at compile time | It can occur at run time or compile time both |
5 | Example | OutOfMemoryError ,IOError | NullPointerException , SqlException |