[英]Exception is caught when Exception is not thrown
我有以下代碼,findbugs 抱怨在狡猾的代碼下“ Exception is caught when Exception is not thrown
”。 我不明白如何解決這個問題。 getPMMLExportable
拋出一個MLPMMLExportException
。
public String exportAsPMML(MLModel model) throws MLPmmlExportException {
Externalizable extModel = model.getModel();
PMMLExportable pmmlExportableModel = null;
try {
pmmlExportableModel = ((PMMLModelContainer) extModel).getPMMLExportable();
} catch (MLPmmlExportException e) {
throw new MLPmmlExportException(e);
}
}
這是一個非常有名的 findbug 警告,
根據官方文檔,這種警告是在
catch(Exception e)
一次捕獲所有類型的異常時也會拋出它,它可以掩蓋實際的編程問題,因此 findbugs 要求您捕獲特定的異常,以便可以拋出表明編程的運行時異常問題。為了更多的理解(以及解決方案),您可以查看官方文檔。
對於您的情況, try
子句中的語句似乎不會拋出您在catch
子句中處理的異常
希望這可以幫助!
祝你好運!
如果您試圖捕獲所有異常並希望避免此問題,則需要將捕獲至少分成 2 個塊。 一種簡單的方法是在一個塊中捕獲運行時異常,在另一個塊中捕獲所有其他異常。
try {
// Do stuff here, like process json, which might throw a json processing error
} catch (RuntimeException e) {
throw new RuntimeException("Couldn't process stuff", e);
} catch (Exception e) {
throw new RuntimeException("Something failed!", e);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.