Unexpected error order in java compilation

While solving the problem on the Internet, I observed the following java behavior, which I found a bit strange. I started by compiling a program along the following lines:

import java.io.*; class WeirdJava { public static void main (String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); HashMap<Integer, Integer> map = new HashMap<Integer,Integer>(); System.out.println("Weird Java"); } } 

Please note that there are two errors in the above program:

  • I did not handle exceptions that BufferedReader can throw.
  • I did not import the standard util library that contains the HashMap .

Now when I try to compile the above program, the java compiler gives an error that it cannot find the HashMap character. Note that an ad with a HashMap starts after a BufferedReader . Then I will add the following import statement to the program:

 import java.util.HashMap; 

When I compile the program again, this time the compiler shows an error

Unregistered IOException must be caught or thrown away

My questions:

  • Why does this error not occur in a previous compilation attempt?
  • The order in which the compilation error occurs does not seem natural. What are the compiler design principles that come into play during this procedure?
+6
source share
1 answer

This is just the order in which the source is checked by the compiler. In particular, the compiler checks the import and solves them before checking the code, which calls methods that can throw checked exceptions.

If you run javac with -verbose , you will notice that the compiler loads the imported classes, in this case BufferedReader and InputStreamReader , then it loads the public API classes such as Object and String :

 [loading ZipFileIndexFileObject[C:\Dev\Java\jdk1.7.0_75\lib\ct.sym(META-INF/sym/rt.jar/java/io/BufferedReader.class)]] [loading ZipFileIndexFileObject[C:\Dev\Java\jdk1.7.0_75\lib\ct.sym(META-INF/sym/rt.jar/java/io/InputStreamReader.class)] ] [loading ZipFileIndexFileObject[C:\Dev\Java\jdk1.7.0_75\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]] [loading ZipFileIndexFileObject[C:\Dev\Java\jdk1.7.0_75\lib\ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]] [checking test.Test] [loading ZipFileIndexFileObject[C:\Dev\Java\jdk1.7.0_75\lib\ct.sym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]] [loading ZipFileIndexFileObject[C:\Dev\Java\jdk1.7.0_75\lib\ct.sym(META-INF/sym/rt.jar/java/lang/System.class)]] [loading ZipFileIndexFileObject[C:\Dev\Java\jdk1.7.0_75\lib\ct.sym(META-INF/sym/rt.jar/java/io/InputStream.class)]] [loading ZipFileIndexFileObject[C:\Dev\Java\jdk1.7.0_75\lib\ct.sym(META-INF/sym/rt.jar/java/io/Reader.class)]] Test.java:11: error: cannot find symbol HashMap<Integer, Integer> map = new HashMap<Integer,Integer>(); 

After reviewing the overview in this link , loading the classes themselves is part of the first phase compilation called "Analysis and Input":

Each tree is passed an Enter, which introduces characters for all definitions found in characters. This must be done before analyzing trees that may refer to these characters. The result of this phase is a To Do list containing the trees that you need to parse and create class files.

+4
source

Source: https://habr.com/ru/post/989966/


All Articles