English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Exceptions and errors are subclasses of the Throwable class. The error indicates a problem mainly caused by the lack of system resources, and our application should not catch these types of problems. Some examples of errors are system crash errors and out of memory errors. Errors mostly occur at runtime because they belong to unchecked types.
Exceptions are problems that may occur at runtime and compile time. They mainly occur in the code written by developers. Exceptions are divided into two categories, such as checked exceptions and unchecked exceptions.
Index | Key | Error | Exception |
---|---|---|---|
1 | Type | Categorized as unchecked types | Categorized as checked and unchecked |
2 | Package | It belongs to java.lang.error | It belongs to java.lang.Exception |
3 | Recoverable/Unrecoverable | This is unrecoverable | It is recoverable |
4 | It cannot occur during compile time | It can occur at the same time during runtime and compile time | |
5 | Example | OutOfMemoryError, IOError | NullPointerException, SqlException |
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); } } }
Output Result
Exception in thread "main" java.lang.StackOverflowError at ErrorExample.ErrorExample(Main.java:42)
public class ExceptionExample { public static void main(String[] args){ int x = 100; int y = 0; int z = x / y; } }
Output Result
java.lang.ArithmeticException: / by zero at ExceptionExample.main(ExceptionExample.java:7)