The as2lib’s exception handling is pretty similar to the Java way of handling exceptions. All basic exception classes reside in the org.as2lib.env.except package. There exists an interface called Throwable. This interface specifies all the operations every throwable/exception should have. Two concrete implementations of this interface are the Exception and FatalException classes. The only difference between these two is the level of fatality. They could be compared with the Exception and Error class respectively in the Java programming language.
One significant difference between the as2lib’s exception handling compared with the default Error class of Flash is the quantity of information offered. The as2lib’s exception handling provides information about the operation that threw the exception as well as information about the cause of the exception. The two operations to get hold of this information are Throwable.getStackTrace() and Throwable.getCause(). The stack trace offers right now only information about the operation that threw the exception and not about all the operations that were executed before the exception has been thrown. This is because arguments.caller.arguments… is not possible. But we are looking forward to offer a full stack trace in future releases.
Read on to see an example of how to write your own custom exception.
Writing exceptions is actually one of the easiest thing to do. You only have to import either org.as2lib.env.except.Exception or org.as2lib.env.except.FatalException and extend one of these classes. Then you implement a constructor that takes three arguments. At first the message then the throwing object and the arguments of the throwing method. The two last arguments are needed to provide stack trace information. You now have to call the constructor of the superclass and pass these three arguments. The concrete example is following:
-
import org.as2lib.env.except.Exception;
-
-
class CustomException extends Exception {
-
public function CustomException(message:String, thrower, args) {
-
super(message, thrower, args);
-
}
-
}
Well, that’s it. The only thing missing in the example is good documentation.
Oh, before I forget it. You can of course customize the Stringifier used to stringify the exception to get a different output as you need it. To do this create a new implementation of the org.as2lib.util.string.Stringifier interface and add an instance of this implementation class as throwable stringifier in the abstract org.as2lib.env.except.ExceptConfig class via the setThrowableStringifier() operation.

3 Responses to “as2lib: Exception Handling”
Hi, I found it seems I failed to catch the Exception which is thrown in an interval call. I am not sure if it is really not caught or is there anything wrong with my usage. So I post the coding herebelow.
I am using the reviewed package of as2lib, not the alpha version.
Herebelow is the coding of three cases(oh, the logic is ridiculous, I use it just to illustrate the phenomenon). First and second are normal, the third one just gives no output.
//============ Normal Case=================
import org.as2lib.env.except.Exception;
function compareStrings(string_1, string_2) {
try {
if(string_1 != string_2) {
throw new Exception(”Strings do not match.”, this, arguments);
}
} catch (e:Exception) {
trace(e.toString());
}
}
compareStrings(”Dog”,”dog”);
//Output:org.as2lib.env.except.Exception: Strings do not match.
//====Interval Call Case Use Error======
var intervalId;
var count = 0;
function compareStrings(string_1, string_2) {
count++;
try {
if(string_1 != string_2 && count > 5) {
clearInterval(intervalId);
throw new Error(”Strings do not match”);
}
} catch (e:Error) {
trace(e.toString());
}
}
intervalId = setInterval(compareStrings, 10, [”Dog”,”dog”]);
//Output: Strings do not match
//===== Interval Call Case Use Exception ====
import org.as2lib.env.except.Exception;
var intervalId;
var count = 0;
function compareStrings(string_1, string_2) {
count++;
try {
if(string_1 != string_2 && count > 5) {
clearInterval(intervalId);
throw new Exception(”Strings do not match.”, this, arguments);
}
} catch (e:Exception) {
trace(e.toString());
}
}
intervalId = setInterval(compareStrings, 10, [”Dog”,”dog”]);
//No output
I knew that the handling of custom exceptions in flash are buggy. We have already posted the bugs we encountered on http://www.as2lib.org/blog/index.php?p=20. But this one is new.
Thus I have myself made a few tests. The easiest thing to do is either use no type in the catch block or to use ‘Error’. But that’s no reals solution but just a dump workaround. I have used the following function for my tests:
import org.as2lib.env.except.Exception;
function compareStrings(string_1, string_2) {
try {
trace(”About to throw an exception!”);
throw new Exception(”Strings do not match.”, this, arguments);
} catch (e:Exception) {
trace(”We caught it!”);
} finally {
clearInterval(intervalId);
}
}
If you call the function using:
compareStrings(”Dog”, “dog”);
everything works fine and the output is:
About to throw an exception!
We caught it!
But as soon as you use intervals the way you did:
var intervalId:Number = setInterval(compareStrings, 10, [”Dog”,”dog”]);
the exception will not be caught. But you can also use intervals in a somehow different manner:
var intervalId:Number = setInterval(this, “compareStrings”, 10, [”Dog”,”dog”]);
What has changed is that the function is now called with scope. Everything else is left unchanged. And it works again as expected.
Exceptions in Flash are definitely something to improve in the next release of Flash. And I hope that all these mysterious issues will be resolved then.
Thank you very much. I actually have a class doing that, so I mingled tips from you and those from http://www.as2lib.org/blog/index.php?p=20, it now works fine.