Forums

weird behavior of catch scoping rules

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

weird behavior of catch scoping rules

Postby jonathandavid » Sat Jan 17, 2009 2:27 am

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?
jonathandavid
 
Posts: 159

Re: weird behavior of catch scoping rules

Postby Charles » Sat Jan 17, 2009 2:35 am

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 46 guests