From 192160541bb31c74468509faf5b19f42b0e4d450 Mon Sep 17 00:00:00 2001 From: Matthias Jaros Date: Mon, 6 Jul 2026 15:42:44 +0200 Subject: Reworked exception handling. Collect now only weird exceptions and print them before results to stderr --- src/main/java/com/it_jaros/jscanner/Scanner.java | 25 +++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'src/main/java/com/it_jaros/jscanner/Scanner.java') diff --git a/src/main/java/com/it_jaros/jscanner/Scanner.java b/src/main/java/com/it_jaros/jscanner/Scanner.java index 55b42fc..63b1d14 100644 --- a/src/main/java/com/it_jaros/jscanner/Scanner.java +++ b/src/main/java/com/it_jaros/jscanner/Scanner.java @@ -107,7 +107,7 @@ public class Scanner implements AutoCloseable { } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { - System.out.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage()); + System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage()); } } @@ -129,10 +129,10 @@ public class Scanner implements AutoCloseable { if (!disableOnlineCheck && !checkHostOnline(host)) { // Unreachable host - return new ScanResult(host, openPorts, filteredPorts, serviceTypes); + return new ScanResult(host, openPorts, filteredPorts, serviceTypes, List.of()); } - List errors = new ArrayList<>(); + List scanFailures = new ArrayList<>(); final AtomicLong portSlotFactory = new AtomicLong(System.nanoTime()); // producer thread ProducerState state = startProducer( @@ -173,6 +173,13 @@ public class Scanner implements AutoCloseable { // sonarcube glücklich machen } } + Exception e = portResult.getException(); + if (e != null) { + scanFailures.add(new ScanFailure(portResult.getPort(), ExceptionInfo.from(e))); + } + } catch (ExecutionException e) { + // something more serious did not work + System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage()); } finally { scan.getThreadCounter().dec(); state.activeWorkers().release(); @@ -180,16 +187,10 @@ public class Scanner implements AutoCloseable { } } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); - } catch (ExecutionException e) { - errors.add(e); } } - if (!errors.isEmpty()) { - System.out.printf("Errors happened during scan of host %s%nErrors:%s -> %s", host, errors.size(), errors); - } - - return new ScanResult(host, openPorts, filteredPorts, serviceTypes); + return new ScanResult(host, openPorts, filteredPorts, serviceTypes, scanFailures); } /** @@ -295,9 +296,11 @@ public class Scanner implements AutoCloseable { result.setState(PortState.FILTERED); } catch (ConnectException ignored) { result.setState(PortState.CLOSED); - } catch (IOException ignored) { + } catch (NoRouteToHostException ignored) { // NoRouteToHostException: this can be safely ignored because the port is closed if a host is unreachable // Will happen a lot when scanning for open ports, so not needed + } catch (IOException e) { + result.setException(e); } finally { scan.getSocketCounter().dec(); socketLimit.release(); -- cgit v1.3.1