Blog RSS Feed Subscribe

Jordi Boggiano

Jordi Boggiano Passionate web developer, specialized in web performance and php. Partner at Nelmio, information junkie and speaker.

Categories

Throwing errors before a super() statement

Lately I was building a class which takes XML as it's configuration, reads part of it and then calls super() with some parameters.

However I wanted to throw an error if the XML did not contain valid settings, and Flex doesn't let you do that. Upon compilation it returns an error saying 1201: A super statement cannot occur after a this, super, return, or throw statement.

Fortunately, it's possible to overcome this by creating a helper function or class that will throw the error for you, so that's what I did with my ErrorThrower class. It is as simple as it can get, and it fools the compiler alright.

package com.seld.errors 
{ 
     public class ErrorThrower 
     { 
         public function ErrorThrower(msg:String, id:int = 0):void 
         { 
             throw new Error(msg, id); 
         } 
     } 
}

Then with that class, instead of doing throw new Exception('foo'); you just do new ErrorThrower(msg); and it will be compiled, error free.

November 27, 2007 // ActionScript

Post a comment

Subscribe to this RSS Feed Comments

2009-07-20 12:21:31

Heke

Heh, Having the same kinda problem and thought about just using a function for that. Don't know if it works, but ErrorThrower class sounds a better idea. Any idea why it's made impossible to throw Errors before super statements?