簡體   English   中英

無法捕獲異常

[英]Cannot catch an exception

以下代碼引發了我的try catch無法處理的NullPointerException 為什么會這樣,我該如何處理該異常?

try
{
    URL url = MyTestApp.class.getResource("deliberately_bad_path/icon.png");
    Image img = Toolkit.getDefaultToolkit().createImage(url);
    ImageIcon icon = new ImageIcon(img);
} catch (Exception e)
{
    System.out.println("Okay it was an Exception");
    e.printStackTrace();
} catch (Throwable  e)
{
    System.out.println("Okay it was a throwable");
    e.printStackTrace();
} 

因此未捕獲到異常,並顯示以下輸出:

捕獲圖像時發生未捕獲的錯誤:
顯示java.lang.NullPointerException
在sun.awt.image.URLImageSource.getConnection(未知源)
在sun.awt.image.URLImageSource.getDecoder(未知源)
在sun.awt.image.InputStreamImageSource.doFetch(未知源)
在sun.awt.image.ImageFetcher.fetchloop(來源不明)
在sun.awt.image.ImageFetcher.run(未知來源)

錯誤原因很可能是您向Toolkit.createImage傳遞了一個空URL。

圖像加載由sun.awt.image.ImageFetcher完成。 它運行在自己的線程中(實際上是從Thread派生的),因此您的catch塊不會捕獲其錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM