Flash 试着抓住不起作用?
我在以下Actionscript 3代码中使用了try-catch块:Flash 试着抓住不起作用?,flash,actionscript-3,error-handling,try-catch,Flash,Actionscript 3,Error Handling,Try Catch,我在以下Actionscript 3代码中使用了try-catch块: try { this._subtitle = new SubtitleController(subtitlePath, _framerate); this._subtitle.addEventListener(Event.COMPLETE, subtitleLoaded); } catch (e:Error) { trace('subtitle not found'); } subtitcontro
try {
this._subtitle = new SubtitleController(subtitlePath, _framerate);
this._subtitle.addEventListener(Event.COMPLETE, subtitleLoaded);
}
catch (e:Error) {
trace('subtitle not found');
}
subtitcontroller
构造函数然后尝试加载subtitpath
,并抛出一个错误#2044:Unhandled ioError
,但try
语句不会捕获该错误。这个错误就像不存在try
语句一样简单地抛出
当然,我可以用
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);
它几乎起作用了,它停止了那个错误,而是抛出了另一个错误
但是,
try catch
块的全部意义不就在于此吗?为什么它不能与try catch一起工作,但它可以与常规事件侦听器一起工作?IOErrors/NetworkErrors是异步错误。当导致它们的方法作为正常运行时错误调用时,不会抛出它们。否则,执行必须完全停止,直到(例如)文件完全加载
基本上,由于调用是异步的
试着…抓住…最后拦住你
有点好。这需要一段时间
用于加载程序确定url的
坏的,然后它发送
IO_错误事件。
-
西奥是正确的;我只想添加使用flash.events
包的IOErrorEvent
类来为不知道该方法的任何人处理这个问题
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest("test.mp3"));
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
function onIOError(e:IOErrorEvent):void
{
trace("An IO Error has occured.\n\n", e);
}
如果您使用的是加载器对象而不是URLLoader,请记住,您需要侦听加载器对象的contentLoaderInfo属性,如下所示
var loader:Loader = new Loader();
addChild(loader);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
loader.load(new URLRequest("path/to/asset"));
function onIOError(e:IOErrorEvent):void
{
trace("An IO Error has occured.\n\n", e);
}
今天我发现有一个名为log.as的类是个坏主意。 从此类抛出的错误不会在try-catch中捕获 我想他们以前都很好。 我一直在使用FlexUnit进行预期错误的单元测试。 我安装了AIR SDK 2.7,最近添加了AIR SDK 2.01(用于另一个项目),这可能引发了冲突 可能是旧SDK的一个特例,或者是关于我的构建环境的一些特定情况
重命名该类为我解决了问题。您可能运气不好,adobe制作的actionscript库非常难看,多种类型的“事件处理程序”模式和异常似乎非常固定。。与原始调用相比,异常/错误似乎经常在事件上广播。。现在有道理了。因此,对于除i/o和网络错误以外的任何其他错误,它应该可以工作,对吗?