The first time I write on java. The program scanned ip-enabled devices and worked on the command line, but Gui and the re-scan button really wanted to. The button was added and all Google with manuals did not help to bypass the error

Error: (64, 27) java: unreported exception java.lang.InterruptedException; must be caught

although judging by the search should have worked with the solution:

try{ rescan(); }catch(IOException e){ e.printStackTrace(); } 

code itself

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.Properties; import java.lang.InterruptedException; import java.nio.charset.*; import java.nio.file.*; import java.awt.*; import javax.swing.*; import java.io.*; import java.io.File; public class solution extends JFrame { private static String ip_value1 = "192.168.1.1"; private static String ip_value2 = "1"; private static String ip_value3 = "ip_value3"; private static String stb_password = "0"; private static String root_password = "0"; private JLabel countLabel; private JButton addCrow; public solution() throws IOException, InterruptedException { super("I Find you by IP"); countLabel = new JLabel("List of Ips:"); addCrow = new JButton("Scan Ip"); JPanel buttonsPanel = new JPanel(new FlowLayout()); //РасставляСм ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Ρ‹ ΠΏΠΎ мСстам add(countLabel, BorderLayout.NORTH); //О Ρ€Π°Π·ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠΈ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚ ΠΏΠΎΠ³ΠΎΠ²ΠΎΡ€ΠΈΠΌ ΠΏΠΎΠ·ΠΆΠ΅ buttonsPanel.add(addCrow); add(buttonsPanel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocation(300,200); setDefaultCloseOperation(EXIT_ON_CLOSE); JTextArea ta = new JTextArea(20,40); getContentPane().add(new JScrollPane(ta)); pack(); try{ta.read(new FileReader("3.txt"),null);}catch(IOException ioe){} initListeners(); } private void initListeners() { addCrow.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try{ rescan(); }catch(IOException e){ e.printStackTrace(); } } }); } private void readFile(){ File f = new File("3.txt"); try{ FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); while(br.ready()){ System.out.println(br.readLine()); } }catch(Exception e){ System.out.println(e); } } void rescan() throws IOException, InterruptedException{ String grep_ip = "echo "+ root_password + "|sudo -S nmap -sP " + ip_value1 + "/24 | grep -oE '\\b[0-9]{1,3}(\\.[0-9]{1,3}){3}\\b' > 1.txt"; ProcessBuilder procBuilder = new ProcessBuilder("/bin/sh", "-c", grep_ip); procBuilder.redirectErrorStream(true); Process process = procBuilder.start(); InputStream stdout = process.getInputStream(); InputStreamReader isrStdout = new InputStreamReader(stdout); BufferedReader brStdout = new BufferedReader(isrStdout); String line = null; while((line = brStdout.readLine()) != null) { System.out.println(line); } int exitVal = process.waitFor(); String grep_ip2 = "echo "+ root_password + "|sudo -S nmap -sP " + ip_value2 + "/24 | grep -oE '\\b[0-9]{1,3}(\\.[0-9]{1,3}){3}\\b' > 2.txt"; ProcessBuilder procBuilder1 = new ProcessBuilder("/bin/sh", "-c", grep_ip2); procBuilder1.redirectErrorStream(true); Process process1 = procBuilder1.start(); InputStream stdout1 = process1.getInputStream(); InputStreamReader isrStdout1 = new InputStreamReader(stdout1); BufferedReader brStdout1 = new BufferedReader(isrStdout1); String line1 = null; while((line1 = brStdout1.readLine()) != null) { System.out.println(line1); } System.out.println("get"+line1); int exitVal1 = process1.waitFor(); System.out.println(ip_value2); String grep_ip3 = "ifconfig | grep -oE \'inet addr:\\b[0-9]{1,3}(\\.[0-9]{1,3}){3}\\b\' > 3.txt"; ProcessBuilder procBuilder2 = new ProcessBuilder("/bin/sh", "-c", grep_ip3); procBuilder2.redirectErrorStream(true); Process process2 = procBuilder2.start(); InputStream stdout2 = process2.getInputStream(); InputStreamReader isrStdout2 = new InputStreamReader(stdout2); BufferedReader brStdout2 = new BufferedReader(isrStdout2); String line2 = null; while((line2 = brStdout2.readLine()) != null) { //line2 = line2.replace ("inet addr:", ""); System.out.println(line2); } int exitVal2 = process2.waitFor(); replace_localhost("3.txt"); replaces("3.txt"); try{ rescan(); }catch(IOException e){ e.printStackTrace(); } } public void actionPerformed(ActionEvent event) { try { if (event.getActionCommand().equals("Scan IPs")) { readFile(); } } catch (Exception e) { } } public static void read_config() throws IOException { Properties props = new Properties(); props.load(new FileInputStream(new File("config.ini"))); ip_value1 = props.getProperty("ip_value1"); ip_value2 = props.getProperty("ip_value2"); ip_value3 = props.getProperty("ip_value3"); stb_password = props.getProperty("stb_password"); root_password = props.getProperty("root_password"); System.out.println(ip_value1); System.out.println(ip_value2); System.out.println(ip_value3); System.out.println(stb_password); System.out.println(root_password); //return ip_value1; } public static void check_ip_diapasone() throws IOException { Runtime.getRuntime().exec("nmap -sP " + ip_value1 + "/24"); System.out.println("nmap -sP " + ip_value1 + "/24"); } public static void replaces(String name_file) throws IOException { String fileName = name_file; String search = "inet addr:"; String replace = "PC: "; Charset charset = StandardCharsets.UTF_8; Path path = Paths.get(fileName); Files.write(path, new String(Files.readAllBytes(path), charset).replace(search, replace) .getBytes(charset)); } public static void replace_localhost(String name_file) throws IOException { String fileName = name_file; String search = "inet addr:127.0.0.1"; String replace = ""; Charset charset = StandardCharsets.UTF_8; Path path = Paths.get(fileName); Files.write(path, new String(Files.readAllBytes(path), charset).replace(search, replace) .getBytes(charset)); } public static String run_command() throws IOException, InterruptedException { ProcessBuilder procBuilder = new ProcessBuilder("ls","-l","/dev"); procBuilder.redirectErrorStream(true); Process process = procBuilder.start(); InputStream stdout = process.getInputStream(); InputStreamReader isrStdout = new InputStreamReader(stdout); BufferedReader brStdout = new BufferedReader(isrStdout); String line = null; while((line = brStdout.readLine()) != null) { System.out.println(line); } int exitVal = process.waitFor(); return "0"; } public static void main(String[] args) throws IOException, InterruptedException { read_config(); check_ip_diapasone(); String command1 = "nmap -sP " + ip_value1 + "/24"; String grep_ip = "echo "+ root_password + "|sudo -S nmap -sP " + ip_value1 + "/24 | grep -oE '\\b[0-9]{1,3}(\\.[0-9]{1,3}){3}\\b' > 1.txt"; ProcessBuilder procBuilder = new ProcessBuilder("/bin/sh", "-c", grep_ip); procBuilder.redirectErrorStream(true); Process process = procBuilder.start(); InputStream stdout = process.getInputStream(); InputStreamReader isrStdout = new InputStreamReader(stdout); BufferedReader brStdout = new BufferedReader(isrStdout); String line = null; while((line = brStdout.readLine()) != null) { System.out.println(line); } int exitVal = process.waitFor(); String grep_ip2 = "echo "+ root_password + "|sudo -S nmap -sP " + ip_value2 + "/24 | grep -oE '\\b[0-9]{1,3}(\\.[0-9]{1,3}){3}\\b' > 2.txt"; ProcessBuilder procBuilder1 = new ProcessBuilder("/bin/sh", "-c", grep_ip2); procBuilder1.redirectErrorStream(true); Process process1 = procBuilder1.start(); InputStream stdout1 = process1.getInputStream(); InputStreamReader isrStdout1 = new InputStreamReader(stdout1); BufferedReader brStdout1 = new BufferedReader(isrStdout1); String line1 = null; while((line1 = brStdout1.readLine()) != null) { System.out.println(line1); } System.out.println("get"+line1); int exitVal1 = process1.waitFor(); System.out.println(ip_value2); String grep_ip3 = "ifconfig | grep -oE \'inet addr:\\b[0-9]{1,3}(\\.[0-9]{1,3}){3}\\b\' > 3.txt"; ProcessBuilder procBuilder2 = new ProcessBuilder("/bin/sh", "-c", grep_ip3); procBuilder2.redirectErrorStream(true); Process process2 = procBuilder2.start(); InputStream stdout2 = process2.getInputStream(); InputStreamReader isrStdout2 = new InputStreamReader(stdout2); BufferedReader brStdout2 = new BufferedReader(isrStdout2); String line2 = null; while((line2 = brStdout2.readLine()) != null) { //line2 = line2.replace ("inet addr:", ""); System.out.println(line2); } int exitVal2 = process2.waitFor(); replace_localhost("3.txt"); replaces("3.txt"); new solution().setVisible(true); } } 

    1 answer 1

    In java, errors are divided into 3 types: checked (checked exceptions), not checked (runtime exceptions), errors of the "system" level (errors). Mistakes that are not checked can be thrown away and caught anywhere. It is usually not recommended to catch Errors and in most cases there is no point, as it is usually impossible to fix anything from the program.

    We check which java.lang.InterruptedException refers to require that methods that throw out such an error declare this fact in the header using the throws keyword. An example of a declaration is also in your code: solution() throws IOException, InterruptedException { . If you call a method that throws such an exception, you need to wrap the call in try and handle the checked error in the catch or also specify in the method header that your method can throw such an error.

    The error you indicated just says that one of the methods can throw out the error java.lang.InterruptedException which you have not handled and not declared in the method header.

    Error: (64, 27) java: unreported exception java.lang.InterruptedException; must be caught

    When you try to β€œcatch” an error using the catch construct, you must take into account the hierarchy between the error classes. The catch(IOException e) construct will catch only errors represented by the IOException class and its heirs. The java.lang.InterruptedException error is inherited from the Exception class and has no relation to the IOException . As a consequence, the catch(IOException e) construct will not be intercepted. You can add another error handling block for InterruptedException :

     try{ rescan(); }catch(IOException e){ e.printStackTrace(); }catch (InterruptedException e){ //ΠΊΠΎΠ΄ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ ошибки }