简体   繁体   中英

Exception is caught when Exception is not thrown

I have the following code and findbugs complains that " Exception is caught when Exception is not thrown " under dodgy code. I do not understand how to solve this. getPMMLExportable throws a 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);
    }
}

This is a very famous findbug warning,

according to official documentation this kind of warning is generated when

  • method uses a try-catch block that catches Exception objects, but Exception is not thrown within the try block.
  • sometimes it also is thrown when we use catch(Exception e) to catch all types of exceptions at once, it could mask actual programming problems, so findbugs asks you to catch specific exception, so that run-time exceptions can be thrown which indicate programming problems.

for more understanding(and the solution as well) you can have look at the official documentation .

for your case it seems that statements in try clause do not throw the exception you are handling in catch clause

hope this helps!

Good luck!

If you're trying to catch all exceptions, and want to avoid this issue, you need to break your catching into at least 2 blocks. An easy way to do this is catch runtime exceptions in one block, and all others in another.

Discussed here as well

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM