This article has participated in the Denver Nuggets Creators Camp 3 “More Productive writing” track, see details: Digg project | creators Camp 3 ongoing, “write” personal impact.

In Java, there are two types of exceptions:

1) Checked: Is an exception Checked at compile time. If some code in a method throws a checked exception, the method must either handle the exception or specify the exception using the THROWS keyword.

For example, consider the following Java program that opens a file in position “C:\test\a.txt” and prints its first three lines. The program cannot compile because the main() function uses FileReader() and FileReader() throws a check exception FileNotFoundException. It also uses the readLine() and close() methods, which also throw IOException checks

import java.io.*;

class Main {
	public static void main(String[] args) {
		FileReader file = new FileReader("C:\\test\\a.txt");
		BufferedReader fileInput = new BufferedReader(file);
		
		// Print the first three lines of the file "C:\test\a.txt"
		for (int counter = 0; counter < 3; counter++) System.out.println(fileInput.readLine()); fileInput.close(); }}Copy the code

Output:

Error: (10.50) Java: unreported exception error java.io.IOException; It must be caught or declared to throw an error :(5.27) Java: failing to report the exception error Java. IO. FileNotFoundException; It must be caught or declared to throw an error :(12.24) Java: unreported exception error java.io.IOException; It must be caught or declared for throwingCopy the code

To fix the above program, we need to either specify the exception list with throws or use a try-catch block. We use throws in the following program. Since FileNotFoundException is a subclass of IOException, we can specify IOException in the throws list and make the above program compile error free.

import java.io.*;

class Main {
	public static void main(String[] args) throws IOException {
		FileReader file = new FileReader("C:\\test\\a.txt");
		BufferedReader fileInput = new BufferedReader(file);
		
		// Print the first three lines of the file "C:\test\a.txt"
		for (int counter = 0; counter < 3; counter++) System.out.println(fileInput.readLine()); fileInput.close(); }}Copy the code

Output: The first three lines of the file “C:\test\a.txt” are as follows

Unchecked: are exceptions that are Unchecked at compile time. In C++, all exceptions are unchecked, so the compiler does not force it to handle or specify exceptions. Programmers should be civilized and specify or catch exceptions. \

Java exceptions under the Error and RuntimeException classes are unchecked exceptions, and all other exceptions under the throwable are checked.

                   +-----------+
           | Throwable |
                   +-----------+
                    /         \
           /           \
          +-------+          +-----------+
          | Error |          | Exception |
          +-------+          +-----------+
       /  |  \           / |        \
         ________/      ______/         \
                            +------------------+
    unchecked     checked    | RuntimeException |
                    +------------------+
                      /   |    |      \
                     _________________/
                       
                       unchecked
Copy the code

Consider the following Java program. It compiles nicely, but throws ArithmeticException at run time. The compiler allows it to compile because ArithmeticException is an unchecked exception.

class Main {
    public static void main(String args[]) {
            int x = 0;
            int y = 10;
            intz = y/x; }}Copy the code

Output:

Should we check for exceptions or not check for exceptions?

The following is a description of the Java documentation

Bottom line criteria:

If a client can reasonably be expected to recover from an exception, make it a checked exception. If the client cannot recover from an exception, make it an unchecked exception

That’s all for this article

💌 welcomes your comments and suggestions in the comments section! 💌

If you really learn something new from this article, like it, bookmark it and share it with your friends. 🤗 and finally, don’t forget ❤ or 📑.