The difference between error and exception in Java: Error errors are errors that cannot be handled by the program. These errors indicate that the fault occurs in the virtual machine itself or when the virtual machine attempts to execute an application, and generally does not require program processing. Exceptions are exceptions that the program itself can handle.
<img src="https://img.php.cn/upload/article/000/000/020/5c9ed8367bf92424.jpg" alt="The difference between error and exception in Java" >
##Error<strong></strong> : Errors are errors that cannot be handled by the program. These errors indicate that the fault occurred in the virtual machine itself or occurred when the virtual machine tried to execute the application, and generally do not require program processing.
java.lang.Throwable class. Error is a situation that cannot be recovered by any processing technology. This will definitely cause the program to terminate abnormally. Error errors are of unchecked type and most occur at runtime. Some examples of Error errors are out of memory errors or system crash errors.
// 通过无限递归演示堆栈溢出错误 class StackOverflow { public static void test(int i) { if (i == 0) return; else { test(i++); } } } public class ErrorEg { public static void main(String[] args) { StackOverflow.test(5); } }Output:
Exception in thread "main" java.lang.StackOverflowError at StackOverflow.test(ErrorEg.java:7) at StackOverflow.test(ErrorEg.java:7) at StackOverflow.test(ErrorEg.java:7) at StackOverflow.test(ErrorEg.java:7) at StackOverflow.test(ErrorEg.java:7) ...
exception<strong></strong>: It is an exception that the program itself can handle.
try,
catch, and
throw keywords.
IOException) at compile time, and the compiler knows about unchecked exceptions (like
ArrayIndexOutOfBoundException) at runtime. It is mostly caused by programs written by programmers.
public class ExceptionEg { public static void main(String[] args) { int a = 5, b = 0; try { int c = a / b; } catch (ArithmeticException e) { e.printStackTrace(); } } }Output:
java.lang.ArithmeticException: / by zero at ExceptionEg.main(ExceptionEg.java:8)Related recommendations: "
Java Tutorial"
This article is about the difference between error and exception in Java Introduction, I hope it will be helpful to friends in need!The above is the detailed content of The difference between error and exception in Java. For more information, please follow other related articles on the PHP Chinese website!