【问题标题】:How to catch Stream Error in ActionScript如何在 ActionScript 中捕获流错误
【发布时间】:2019-03-27 08:24:04
【问题描述】:

我有以下代码,它尝试GET 一个公共托管 (AWS S3) 文件。

private function ShowS3Message():void
{
    // Attempt to download file from AWS
    var descriptor:XML = NativeApplication.nativeApplication.applicationDescriptor;
    var ns:Namespace = descriptor.namespaceDeclarations()[0];
    var url:String = "https://s3.amazonaws.com/some-url/file-" + descriptor.ns::versionLabel.split(".").join("-") + ".txt";
    var urlRequest:URLRequest = new URLRequest(url);

    // Set up callback function
    try{
        var loader:URLLoader = new URLLoader(); 
        loader.addEventListener(Event.COMPLETE, awsFetchCallback);                      
        loader.load(urlRequest);
    }catch(error:Error){}   
}

这是回调函数:

/**
 * Callback function for AWS message file
 */
private function awsFetchCallback(event:Event):void
{
    var data = event.target.data;

    // show dialog
    var msb:InformationMessageBox = new InformationMessageBox();
    msb.mText = data;
    msb.open(this, true);
}

当文件存在时,没有问题,代码运行正常。 当文件不存在时,这会引发 StreamError,尽管有 catch 块。

我错过了什么?

【问题讨论】:

    标签: actionscript-3 io actionscript http-get


    【解决方案1】:

    你应该捕获IO错误事件,当文件不存在时不会抛出异常。

    loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

    然后创建自己的错误处理函数。

    这里的文档中的更多详细信息: https://help.adobe.com/fr_FR/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html

    如果您只是想消除错误(因为您似乎知道文件有时可能不存在),那么创建一个空的错误事件处理程序就足够了。

    【讨论】:

    • 如果我只想淹没错误怎么办?我知道该文件有时不存在。
    猜你喜欢
    • 2011-02-26
    • 2018-12-08
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 2011-06-18
    • 2019-11-08
    • 2010-11-21
    相关资源
    最近更新 更多