gpt4 book ai didi

java - 处理我的代码中未发生的异常,我应该怎么办?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:31:18 25 4
gpt4 key购买 nike

我有一个 JavaFX 应用程序,可以获取节点的屏幕截图/快照。获取屏幕截图的代码如下所示:

WritableImage image = webView.snapshot(null, null);

其中 webView 是一个 javafx.scene.web.WebView。通常这工作正常,但是当 WebView 太大(我的意思是,大约为 10240x5548 像素)时,我得到这个异常:

java.lang.NullPointerException
at com.sun.prism.impl.ps.BaseShaderContext.initLCDBuffer(BaseShaderContext.java:703)
at com.sun.prism.impl.ps.BaseShaderContext.validateLCDBuffer(BaseShaderContext.java:725)
at com.sun.prism.impl.ps.BaseShaderGraphics.initLCDSampleRT(BaseShaderGraphics.java:1925)
at com.sun.prism.impl.ps.BaseShaderGraphics.drawString(BaseShaderGraphics.java:2056)
at com.sun.javafx.webkit.prism.WCGraphicsPrismContext$10.doPaint(WCGraphicsPrismContext.java:936)
at com.sun.javafx.webkit.prism.WCGraphicsPrismContext$Composite.paint(WCGraphicsPrismContext.java:1500)
at com.sun.javafx.webkit.prism.WCGraphicsPrismContext$Composite.paint(WCGraphicsPrismContext.java:1485)
at com.sun.javafx.webkit.prism.WCGraphicsPrismContext.drawString(WCGraphicsPrismContext.java:948)
at com.sun.webkit.graphics.GraphicsDecoder.decode(GraphicsDecoder.java:299)
at com.sun.webkit.graphics.WCRenderQueue.decode(WCRenderQueue.java:92)
at com.sun.webkit.WebPage.paint2GC(WebPage.java:734)
at com.sun.webkit.WebPage.paint(WebPage.java:701)
at com.sun.javafx.sg.prism.web.NGWebView.renderContent(NGWebView.java:96)
at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2053)
at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1945)
at com.sun.javafx.tk.quantum.QuantumToolkit$5.draw(QuantumToolkit.java:1393)
at com.sun.javafx.tk.quantum.QuantumToolkit$5.run(QuantumToolkit.java:1429)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
at java.lang.Thread.run(Thread.java:748)

这种情况并不常见,但我仍然愿意处理这种情况,即使只是告诉用户不支持这种分辨率。问题是堆栈跟踪根本没有触及我的代码。我的代码中没有一行。我猜这是一个由我调用的东西触发的线程,它立即返回,稍后在线程崩溃时返回。

我的应用程序已经是多线程的,因此,我有一个通用的错误处理程序,如下所示:

public class ErrorHandler implements Thread.UncaughtExceptionHandler {
public ErrorHandler(String appName) {
super();
// init stuff.
}

@Override
public void uncaughtException(Thread t, Throwable e) {
// Oh my! something went wrong.
e.printStackTrace(); // We still want to see the exception, specially while developing.
}
}

我在我的应用启动后立即设置如下:

ErrorHandler errorHandler = new ErrorHandler(APP_NAME);
Thread.setDefaultUncaughtExceptionHandler(errorHandler);

这往往会起作用,但由于某种原因,它不适用于该线程。

有什么想法可以让我了解情况并采取相应措施吗?

注意:我发布了一个相关问题,在该问题中我实际上从生产中获取异常报告,看起来像一个大屏幕截图,但我无法触发该异常,而是触发了这个异常。这个问题在这里:How to work around Unrecognized image loader: null in JavaFX?

更加努力地弄清楚发生了什么,最终我将其发送到 stderr:

Outstanding resource locks detected:
D3D Vram Pool: 402,867,712 used (75.0%), 500,823,400 target (93.3%), 536,870,912 max
21 total resources being managed
average resource age is 37.2 frames
0 resources at maximum supported age (0.0%)
11 resources marked permanent (52.4%)
5 resources have had mismatched locks (23.8%)
5 resources locked (23.8%)
12 resources contain interesting data (57.1%)
0 resources disappeared (0.0%)

我不确定那里发生了什么,但它可能是相关的。

最佳答案

您可以在执行快照之前引入一个简单的验证。

if (webView.getHeight() > 1000 || webView.getWidth() > 1000){
throw new Exception("Web view dimension too big for capturing a snapshot!");
}
WritableImage writImage = webView.snapshot(null, null);

如果您不想硬编码 webView 可以拥有的最大值,您可以只获取当前屏幕边界并与它们进行比较。

Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
if (webView.getHeight() > primaryScreenBounds.getHeight() || webView.getWidth() > primaryScreenBounds.getWidth()){
throw new Exception("Webview exceeds screen dimensions!");
}

关于java - 处理我的代码中未发生的异常,我应该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49323895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com