# V6051. Use of jump statements in 'finally' block can lead to the loss of unhandled exceptions.

The analyzer has detected a return/break/continue or other statement of this type used inside a 'finally' block. Such use of these statements may result in losing an unhandled exception thrown in a 'try' or 'catch' block. As stated by JLS [14.20.2], if the 'finally' block terminates in such a way, all exceptions thrown in 'try' or 'catch' will be suppressed.

Consider the following example:

    int someMethod(int a, int b, int c) throws SomeException
    {
      int value = -1;
      ...
      try
      {
        value = calculateTransform(a, b, c);
        ...
      }
      finally
      {
        System.out.println("Result of someMethod()");
        return value;                                  // <=
      }
    }

Even though its signature says it may throw an exception, the 'someMethod' method will never actually do that because executing the 'return' statement will suppress that exception and it will never leave the method body.

Programmers may deliberately use this technique to suppress exceptions. If that is the case, delete the exception-throwing statement in the method signature to prevent the analyzer from issuing a warning.

     int someMethod(int a, int b, int c)
    {
      int value = -1;
      ...
      try
      {
        value = calculateTransform(a, b, c);
        ...
      }
      finally
      {
        System.out.println("Result of someMethod()");
        return value;
      }
    }

Let's modify the previous example a bit:

    int someMethod(int a, int b, int c) throws SomeException
    {
      int value = -1;
      ...
      try
      {
        value = calculateTransform(a, b, c);
        ...
      }
      catch (SomeException se)
      {
        ...
        throw se;
      }
      finally
      {
        System.out.println("Result of someMethod()");
        return value;                                  // <=
      }
    }

This code will trigger the warning too. Here we have an exception handler named 'SomeException', which performs some actions and then re-throws the exception downstream. After that, the 'finally' block terminates and the function returns 'value'. And what about the exception? Well, after it is re-thrown in the handler, it will never leave the method.

To fix that, we should change the code as follows:

    int someMethod(int a, int b, int c) throws SomeException
    {
      int value = -1;
      ...
      try
      {
        value = calculateTransform(a, b, c);
        ...
      }
      catch (SomeException se)
      {
        ...
        throw se;
      }
      finally
      {
        System.out.println("Result of someMethod()");
      }
      return value;
    }

Now, whenever an exception is thrown, it is guaranteed to be re-thrown outside the 'someMethod' method, just as suggested by the method signature.

This diagnostic is classified as:

CWE-584 CERT-ERR04-J

You can look at examples of errors detected by the V6051 diagnostic.