From 1610602884f492b9d82fde3b258b0a4e28573753 Mon Sep 17 00:00:00 2001 From: ZIHAN DAI <99155080+PDGGK@users.noreply.github.com> Date: Tue, 24 Mar 2026 00:20:50 +1100 Subject: [PATCH] Merge pull request #28699 from PDGGK:fix/highgui-system-exit Replace System.exit(-1) with exceptions in HighGui.java (#28696) #28699 ## Summary Library code should never call System.exit() as it kills the entire JVM. Replaced all 3 instances with appropriate exceptions. Closes #28696 ## Changes - `imshow()` with empty image: `System.exit(-1)` -> `throw new IllegalArgumentException("Image is empty")` - `waitKey()` with no windows: `System.exit(-1)` -> `throw new IllegalStateException("No windows created. Call imshow() first")` - `waitKey()` with null window image: `System.exit(-1)` -> `throw new IllegalStateException("No image set for window: ... Call imshow() first")` - `InterruptedException` catch: `printStackTrace()` -> `Thread.currentThread().interrupt()` --- .../highgui/misc/java/src/java/highgui+HighGui.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/modules/highgui/misc/java/src/java/highgui+HighGui.java b/modules/highgui/misc/java/src/java/highgui+HighGui.java index 7a25095166..e986618fb4 100644 --- a/modules/highgui/misc/java/src/java/highgui+HighGui.java +++ b/modules/highgui/misc/java/src/java/highgui+HighGui.java @@ -43,8 +43,7 @@ public final class HighGui { public static void imshow(String winname, Mat img) { if (img.empty()) { - System.err.println("Error: Empty image in imshow"); - System.exit(-1); + throw new IllegalArgumentException("Image is empty"); } else { ImageWindow tmpWindow = windows.get(winname); if (tmpWindow == null) { @@ -113,8 +112,7 @@ public final class HighGui { // If there are no windows to be shown return if (windows.isEmpty()) { - System.err.println("Error: waitKey must be used after an imshow"); - System.exit(-1); + throw new IllegalStateException("No windows created. Call imshow() first"); } // Remove the unused windows @@ -145,8 +143,7 @@ public final class HighGui { win.lbl.setIcon(icon); } } else { - System.err.println("Error: no imshow associated with" + " namedWindow: \"" + win.name + "\""); - System.exit(-1); + throw new IllegalStateException("No image set for window: \"" + win.name + "\". Call imshow() first"); } } @@ -158,6 +155,7 @@ public final class HighGui { } } catch (InterruptedException e) { e.printStackTrace(); + Thread.currentThread().interrupt(); } // Set all windows as already used