Last Updated: February 25, 2016
·
5.982K
· jaimeph

Sonar: Preserve Stack Trace

Sonar: Preserve Stack Trace

New exception is thrown in catch block, original stack trace may be lost

} catch (XPathExpressionException e) {
    throw new SAMLNoFoundException(e.getMessage(), e.fillInStackTrace());
}

Solution:

} catch (XPathExpressionException e) {
    throw e;
}

2 Responses
Add your response

Nested exception constructor doesn't works for Sonar default rule?

What if you want to explicitely throw SAMLNoFoundException (or WhateverMyException) and not XPathExpressionException?

over 1 year ago ·

Good question, try:

} catch (XPathExpressionException e) {
    throw new SAMLNoFoundException("MESSAGE", e);
}

It does not fail.

over 1 year ago ·