Home  >  Article  >  Java  >  The difference between error and exception in Java

The difference between error and exception in Java

藏色散人
藏色散人Original
2019-03-30 10:48:457576browse

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.

Error and exception are both subclasses of the

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.

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

try, catch, and throw keywords.

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 (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!

Statement:
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