Hi all,

sorry for this slightly OT message.

I was wondering what the best way is to test for a method throwing an
error. I have an Account class that takes a number:int and a
name:String as constructor params. If an Account is instantiated with
a null name, I want to throw an Error (IllegalArgumentException).

In my test, I instantiate a new Account with a null name inside a
try/catch and fail immediately after the instantiation. However this
does not seem to work. The fail never executes and the test passes.  
See example:

var errorMessage:String = "Account constructor should throw
IllegalArgumentException when passing 'null' name.";
try {
  var a:Account = new Account(100, null);
  fail(errorMessage);
}
catch (e:Error) {}

The only way I can get this to work, is to check if the errorMessage
in the catch block is the same as the error message in my test and
then call fail() again.

var errorMessage:String = "Account constructor should throw
IllegalArgumentException when passing 'null' name.";
try {
  var a:Account = new Account(100, null);
  fail(errorMessage);
}
catch(e:Error) {
  if (e.message == errorMessage) {
    fail(errorMessage);
  }
}

Seems a bit weird. The fail() in the try block is catched by the catch
block. I thought this worked in AS2? Am I missing out on something or
doing something wrong here?

thx in advance.

regards,
Christophe

Reply via email to