Home> Java> javaTutorial> body text

The difference between error and exception in Java

藏色散人
Release: 2019-03-30 10:50:26
Original
7434 people have browsed it

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.

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 occurred in the virtual machine itself or occurred when the virtual machine tried to execute the application, and generally do not require program processing.

Error and exception are both subclasses of the

java.lang.Throwableclass. 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); } }
Copy after login

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) ...
Copy after login

exception: It is an exception that the program itself can handle.

Exceptions are conditions that occur at runtime and may cause the program to terminate. However, they can be restored using the

try,catch, andthrowkeywords.

Exceptions are divided into two categories: checked exceptions and unchecked exceptions. The compiler knows about checked exceptions (like

IOException) at compile time, and the compiler knows about unchecked exceptions (likeArrayIndexOutOfBoundException) 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(); } } }
Copy after login

Output:


java.lang.ArithmeticException: / by zero at ExceptionEg.main(ExceptionEg.java:8)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!