0

我正在使用 Amazon AWS SDK 从 S3 下载图像。有时,当找不到图像时,会引发异常“AmazonS3Exception:状态代码:404”。但是,这似乎是一个不应使应用程序崩溃的异常。如何处理此异常,以免应用程序崩溃?抱歉,我是 Java 和 Android 的菜鸟。

4

2 回答 2

0

要跟进 type-a1pha 的回答:

如果您想优雅地处理异常,您将使用 try-catch 语句。它的工作原理是这样的:

try {
    // Here you put the code that may throw an exception
} catch (AmazonS3Exception e) {
    // Looks like we errored out, log the exception and
    // tell the user that we 404'd
    Log.e(TAG, "Error fetching file from Amazon S3", e);
    Toast.makeText(context, "Error 404 while fetching file: file not found", Toast.LENGTH_SHORT).show();
    // Insert any other code you need here to recover from the error
} finally {
    // Note that the finally part is optional but useful if you want
    // to do something after the try-catch statement is finished
    // for example, if you were using an inputStream:
    inputStream.close();
}
于 2013-07-25T19:53:47.930 回答
0
try{
    //code throwing exception
} catch (AmazonS3Exception) {}
于 2013-07-25T19:20:12.067 回答