JVM Knowledge

The rules herein cover the try-catch and try-catch-finally statements (§14.20.1, §14.20.2). Since the try-with-resources statement (§14.20.3) is defined by translation to a try-catch-finally statement, no special rules need to be provided for it.

These rules apply to every try statement (§14.20), whether or not it has a finally block:

  • V is [un]assigned before the try block iff V is [un]assigned before the try statement.

  • V is definitely assigned before a catch block iff V is definitely assigned before the try block.

  • V is definitely unassigned before a catch block iff all of the following are true:

    • V is definitely unassigned after the try block.
    • V is definitely unassigned before every return statement that belongs to the try block.
    • V is definitely unassigned after e in every statement of the form throw e that belongs to the try block.
    • V is definitely unassigned after every assert statement that occurs in the try block.
    • V is definitely unassigned before every break statement that belongs to the try block and whose break target contains (or is) the try statement.
    • V is definitely unassigned before every continue statement that belongs to the try block and whose continue target contains the try statement.

If a try statement does not have a finally block, then this rule also applies:

  • V is [un]assigned after the try statement iff V is [un]assigned after the try block and V is [un]assigned after every catch block in the try statement.

If a try statement does have a finally block, then these rules also apply:

  • V is definitely assigned after the try statement iff at least one of the following is true:

    • V is definitely assigned after the try block and V is definitely assigned after every catch block in the try statement.
    • V is definitely assigned after the finally block.
  • V is definitely unassigned after the try statement iff V is definitely unassigned after the finally block.

  • V is definitely assigned before the finally block iff V is definitely assigned before the try statement.

  • V is definitely unassigned before the finally block iff all of the following are true:

    • V is definitely unassigned after the try block.
    • V is definitely unassigned before every return statement that belongs to the try block.
    • V is definitely unassigned after e in every statement of the form throw e that belongs to the try block.
    • V is definitely unassigned after every assert statement that occurs in the try block.
    • V is definitely unassigned before every break statement that belongs to the try block and whose break target contains (or is) the try statement.
    • V is definitely unassigned before every continue statement that belongs to the try block and whose continue target contains the try statement.
    • V is definitely unassigned after every catch block of the try statement.