PDA

View Full Version : Extending Error



lancehalberd
June 24th, 2007, 11:06 PM
Hello,

I would like to extend Error to add some desired functionality to it, but I've run into a couple of funny issues:

I call the new class DetailedError. If I have code like this:



try{
throw new DetailedError(...);
}catch(e:DetailedError){

}


The catch statement is not executed, though it does work if I just set e as "Error". Unfortunately, one of the things I do in DetailedError is override the toString method, and when I call toString on e, it uses the Error toString method.
This brings me to the second oddity. When I try to use the modifier override on the toString method, I get an error saying I must override a method, as if Error does not have a toString method, though it clearly does.

I'm pretty new to AS 3, so I'm probably missing some obvious things (like the fact that Error is at the top level may give it some special status regarding extending it etc.)

Any help on this would be appreciated.

TheCanadian
June 24th, 2007, 11:26 PM
The override attribute is only used for methods inherited through the class itself (not the class prototype). Since the toString method is defined in the prototype object, you can simply write the toString function in your class as you would any other method without having to worry about override.

As for why the catch statement doesn't work is a mystery at the moment. Could you post your class?

Here's a quick working example:

package {
public class DetailedError extends Error {
public function toString():String {
return "DetailedError instance";
}
}
}

try {
throw new DetailedError();
} catch(e:DetailedError) {
trace(e); //DetailedError instance
}

Dazzer
June 24th, 2007, 11:39 PM
Just a guess: Forgot to override the clone() method?

edit: My mistake. I misread as Event not Error. Oopsies :)