4

My Flash program's loading a SWF that contains user code which has been compiled in real time. Because it's user code, it may throw exceptions. Unfortunately, I can't seem to catch the exceptions. This code doesn't work:

this._loader = new Loader();
this._loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfCompleteHandler);
this._loader.loadBytes(swfByteArray, loaderContext);

However, the debug player shows this in the unhandled exception dialog:

[Fault] exception, information=Error: Test error message
at global$init() [User_Code:3]

How do I catch an exception in global$init() of a loaded SWF? I've tried adding UNCAUGHT_ERROR event listeners to every loader and loaderInfo I can find... but none of them trigger when the exception is thrown from the loaded SWF's global$init(). Thanks in advance.

Community
  • 1
  • 1
DoomGoober
  • 1,533
  • 14
  • 20
  • Is your onUncaughtError function executed? – net.uk.sweet Jun 23 '14 at 13:02
  • 1
    Unfortunately, onUncaughtError function does not execute when loading a SWF that throws in its global$init. onUncaughtError does execute if the exception is thrown anywhere else. Thanks. – DoomGoober Jun 24 '14 at 03:58
  • Have you tried putting the loadBytes() line of code inside a try...catch? – C. Parcell Aug 22 '14 at 15:17
  • Did you eventually null'ed the loader in the swfCompleteHandler function? – zyexal Sep 21 '14 at 01:03
  • I am not sure but you can use try .. catch for each and every code which is handling loading and execution of the User SWF. But something will be happen in User's Code which execution started internally via event or any other way then we can't handle what one. – Mrugesh Feb 25 '15 at 21:27

1 Answers1

0

In cases where I could, I injected a try/catch into the user's code. For example, if the user code is simply:

trace("Hello");

My program modifies the code string to be:

try {
trace("Hello");
catch (Error error) { DoSomething(); }

However, this doesn't always work. For example, if the users' code is:

function output():void
{
    throw new Error("Error!");
}
output();

Then the trick above doesn't work. Instead, I inject code like so:

startUserCode();
function output():void
{
    throw new Error("Error!");
}
output();
endUserCode();

If endUserCode() doesn't get hit, I assume that an exception was thrown. I just don't know what exception. A nasty user could insert a random "return" into their code and endUserCode won't get hit but that's an edge case I decided to simply not handle.

DoomGoober
  • 1,533
  • 14
  • 20