1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

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()`
This commit is contained in:
ZIHAN DAI
2026-03-24 00:20:50 +11:00
committed by GitHub
parent 4413c953d1
commit 1610602884
@@ -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