Please go to DobbyKim’s Question of the Day for more questions

A:

First, the Error and Exception classes both inherit from the Throwable class.

  • Error

    Error indicates system-level errors, which are generated and thrown by VMS. Common VM errors include OutOfMemoryError and StackOverflowError.

    These two kinds of mistakes are required to be grasped.

    StackOverflowError is a StackOverflowError that can occur when a recursive function is called without limitation. Base case is the base case that can occur when a recursive function is called.

    Example program:

    public class StackOverflowErrorTest {
    	public static void foo(a){
    		System.out.println("StackOverflowError");
    		foo();
    	}
    	public static void main(String[] args) { foo(); }}Copy the code

    This program causes a StackOverflowError to be thrown

    OutOfMemoryError is a heap memory overflow error. The possible causes of OutOfMemoryError are as follows:

    1. The memory value of the JVM startup parameter is too small
    2. There is an infinite loop in the code resulting in too many object entities
    3. The amount of data loaded in memory is too large, and taking too much data from the database at one time can also cause heap overflow
    4. The collection class has references to objects that are not emptied after use, making it impossible for the JVM to recycle

    Sample program:

    public class OutOfMemoryErrorTest {
    
    	public static void main(String[] args) {
    		while (true) {new Thread(() -> {
    				try {
    					Thread.sleep(1000);
    				} catch(InterruptedException e) { } }).start(); }}}Copy the code

    This example code is an infinite loop, constantly creating new threads, which runs with an OutOfMemoryError.

  • Exception

    Exception is an Exception. In layman’s terms, it means something that would not happen if the program worked properly.

    Exception can be classified as

    • RuntimeException
    • Non-runtime exception

    Or it can be divided into:

    • CheckedException (CheckedException)
    • UncheckedException (UncheckedException)

    In fact, runtime exceptions are exceptions that go unchecked.

    What is a RuntimeException, or an UncheckedException?

    In layman’s terms, unchecked exceptions are those caused by programmers not checking code carefully, such as null Pointers, arrays out of bounds, and so on. These exceptions can usually be avoided during coding. Also, I can throw a runtime exception directly in the code, and the program will compile without error:

    public class Test {
        public static void main(String[] args) {
            throw new IllegalArgumentException("wrong"); }}Copy the code

    What are anomalies being examined?

    Checked exceptions are exceptions that are mandatory to check at compile time. Checked exceptions cannot pass compilation unless they are caught using either a try-catch statement or thrown upwards using throws. Common with checked exceptions have: FileNotFoundException, SQLException, etc