Return Inside Finally Block

Thank you for visiting OWASP.org. We have migrated our community to a new web platform and regretably the content for this page needed to be programmatically ported from its previous wiki page. There’s still some work to be done.

Description

Returning from inside a finally block will cause exceptions to be lost.

A return statement inside a finally block will cause any exception that might be thrown in the try block to be discarded.

Risk Factors

TBD

Examples

In the following code excerpt, the IllegalArgumentException will never be delivered to the caller. The finally block will cause the exception to be discarded.

    try {
      ...
      throw IllegalArgumentException();
    }
    finally {
      return r;
    }

References

Note: A reference to related CWE or CAPEC article should be added when exists. Eg:

NOTOC

Category:OWASP ASDR Project Category:Error Handling Vulnerability Category:Java Category:Implementation Category:Code Snippet Category:Vulnerability