I’m using a try-catch block in the following Actionscript 3 code:
try { this._subtitle = new SubtitleController(subtitlePath, _framerate); this._subtitle.addEventListener(Event.COMPLETE, subtitleLoaded); } catch (e:Error) { trace('subtitle not found'); }
The SubtitleController constructor then tries to load the subtitlePath and throws an Error #2044: Unhandled ioError, but the error is not caught by the try statement. The error is simply thrown like no try statement ever existed.
Sure, I can replace that code with
this._subtitle.addEventListener(IOErrorEvent.IO_ERROR, function (ev:Event) { trace('subtitle not loaded'); }); this._subtitle = new SubtitleController(subtitlePath, _framerate); this._subtitle.addEventListener(Event.COMPLETE, subtitleLoaded);
and it almost works, it stops trowing that error, but throws another one instead.
But isn’t the whole point of try-catch blocks to do that? Why does it not work with the try-catch, but it does work with a regular event listener?
IOErrors/NetworkErrors are asynchronous errors. They are not thrown when the method causing them is called as normal runtime errors. Otherwise the execution would have have to stop completely until a (for example) a file is completely loaded …