Page 1 of 1

weird behavior of catch scoping rules

PostPosted: Sat Jan 17, 2009 2:27 am
by jonathandavid
Consider the current behavior of Cobra in three similar programs.

class Program1

def main is shared

try
pass
catch ex as Exception
pass

try
pass
catch ex as FormatException # Compiler error: redeclaration of ex from Exception to FormatException
pass



class Program2

def main is shared

try
pass
catch ex as Exception
pass

try
pass
catch ex as Exception # Works, since type of ex is preserved
pass


class Program3

def main is shared

try
pass
catch ex as Exception
pass

try
pass
catch ex # Compiler error: cannot find type of ex
pass


I can understand the error in Program1, taking into account that Cobra's loose block scoping rules are based on Python.

With that in mind, I can understand Program 2 as well, although I would expect it to issue a warning that said something like "you can remove as Exception, since ex is already of that type".

But what I don't understand is Program3. From programs 1 and 2 it is clear that ex is still on scope, so then why is it that the compiler chokes when it finds "ex" without a type?

Re: weird behavior of catch scoping rules

PostPosted: Sat Jan 17, 2009 2:35 am
by Charles
It's because Cobra is "seeing" it as the form:
catch <Type>

Which is a supported form of catching: You can catch the type without putting a reference into a variable.

I guess I'm okay with "catch <var>" so if you want to ticket that, please do so.

I'm doubtful about adding the warning for case 2 as the redundancy isn't really for the benefit of the variable, but for the benefit or reading that particular try...catch which may have multiple catches and may be far removed from the first try.