Re: Why can't finally{} acces in-try{} variables?

From: Dale King (kingd[at]tmicha[dot]net)
Date: 02/07/04


Date: Sat, 7 Feb 2004 16:16:45 -0500


"Bryan Bullard" <the_bullardsREMOVETHIS@sbcglobal.net> wrote in message
news:q_RUb.1254$eo.1055@newssvr23.news.prodigy.com...
> > Bull***. From exception handling point of view, try{} variables
> > _should_ be visible inside the finally{} block: the whole point
> > of finally{} is to clean them up.
>
> perhaps. however, scope rules must be enforced. to make such exceptions
> may introduce needless ambiguities in the spec.

It really has less to do with scoping rules than it does with definite
assignment. Definite assignment says that a local variable must be
definitely assigned a value before its value is accessed. Sun considers this
so important that they devoted an entire chapter of the Java Language
Specification to the subject:

http://java.sun.com/docs/books/jls/second_edition/html/defAssign.doc.html#25
979

If a variable is declared inside a try block there is no way to know if the
variable was definitely assigned or not. The JLS requires that a compiler be
quite conservative in its flow analysis, which would mean for the try block
to assume that an exception could occur anywhere in a try block. The only
way out is to definitely assign a value outside of the try block and then
you know it was definitely assigned a value in a catch or finally block.
Since we have such conservative definite assignment rules, it is convenient
to enforce them with scoping rules.

But the important thing is that the motivation is definite assignment, not
the scoping rules.

--
  Dale King