Java exception handling leads to
Suppose you want to write a Java program that reads a line of text entered by the user and displays the text in the terminal.
The procedure is as follows:
1 import java.io.*;
2 public class EchoInput {
3 public static void main(String args[]){
4 System.out.println("Enter text to echo:");
5 InputStreamReader isr = new InputStreamReader(System.in);
6 BufferedReader inputReader = new BufferedReader(isr);
7 String inputLine = inputReader.readLine();
8 System.out.println("Read:" + inputLine);
9 }
10 }
Parsing the above code, in the Echoinput class, line 3rd declares the main method, line 4th prompts the user for text, and the 5th and 6 lines set BufferedReader to connect to InputStreamReader, and InputStreamReader It is connected to the standard input stream system.in; line 7th reads a line of text, and line 8th displays the text with the standard output stream System.out.
On the surface, there is no problem with the above procedure, but in fact, the Echoinput class may be completely problematic. To read input correctly when calling the ReadLine method on line 7th, these assumptions must be true: assuming the keyboard is valid, the keyboard communicates with the computer, and the keyboard data is transferred from the operating system to the Java Virtual machine, and the Inputreader is transferred from the Java Virtual machine.
Most of the above assumptions are true, but not necessarily. To do this, Java uses an exception method to respond to possible errors and take steps to correct them. In this case, if you attempt to compile the above code, you see the following information:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException
at EchoInput.main(EchoInput.java:7)
As you can see, line 7th calls the ReadLine method may be wrong: if so, it generates IOException to record the failure. The compiler error is telling you that you need to change the code to solve this potential problem. The same information can be seen in the JDK API documentation. We can see the ReadLine method, as shown in Figure 1.
Figure 1. JDK API documentation for the ReadLine method of the BufferedReader class
As can be seen from fig. 1, ReadLine methods sometimes produce ioexception. How do I handle a potential failure? The compiler needs to "capture" or "declare" IOException.
Capture (catch) means that the error is intercepted when the ReadLine method generates an error, and the problem is processed and logged. A "declaration (declare)" means that an error can raise a IOException and notify any code that invokes the method: An exception may occur.
To catch exceptions, you must add a special "process code block" to receive and process IOException. The procedure was then read as follows:
1 import java.io.*;
2 public class EchoInputHandle {
3 public static void main(String args[]){
4 System.out.println("Enter text to echo:");
5 InputStreamReader isr = new InputStreamReader(System.in);
6 BufferedReader inputReader = new BufferedReader(isr);
7 try{
8 String inputLine = inputReader.readLine();
9 System.out.println("Read:" + inputLine);
10 }
11 catch(IOException exc){
12 System.out.println(“Exception encountered: ” + exc);
13 }
14 }
15 }
The newly added code block contains the keyword try and catch (line 7,10,11,13), which indicates that the input is to be read. If successful, it runs normally. If an error is read, the problem is captured (represented by the IOException object) and the appropriate action is taken. In this case, the method used is to output an exception.
If you are not prepared to capture IOException and declare exceptions only, you can specifically specify that the main method may be wrong, and specifically indicate that a ioexception may be generated. The procedure was then read as follows:
1 import java.io.*;
2 public class EchoInputDeclare {
3 public static void main(String args[]) throws IOException{
4 System.out.println("Enter text to echo:");
5 InputStreamReader isr = new InputStreamReader(System.in);
6 BufferedReader inputReader = new BufferedReader(isr);
7 String inputLine = inputReader.readLine();
8 System.out.println("Read:" + inputLine);
9 }
10 }
From the simple example above, we can see that exception handling cannot be overlooked in Java code development.