如何知道AWS API抛出了哪些异常


How to know what exceptions AWS APIs throws

根据文档中的此页面,您可以在新的AWS API中使用异常来了解何时发生错误;事实上,他们甚至展示了一个带有createBucket函数的示例,然而,当查看API文档时,它并没有提到它可以抛出什么异常。

有没有一种有据可查的方法可以找到答案?

如果你要按函数查找抛出的异常类型,你只需要按函数查看文档,例如函数createPresignedUrl抛出Aws''Common''exception''InvalidArgumentException。请注意,有些函数不会抛出异常。

所有异常都是类,当抛出异常时,使用的是"new"关键字。要构建自定义异常,您需要从主异常扩展它们!这意味着什么?

你不必担心,就这样做吧。

捕捉到全局异常

try
{
    # code here
}
# if the exception type is match, script going inside this part.
catch (BucketAlreadyExistsException $e) 
{
    echo 'That bucket already exists! ' . $e->getMessage() . "'n";
}
# Catching many type you want.
catch (OverflowException $e)
{
    echo $e->getMessage . " - " . $e->getCode();
}
# Like a else, this part is catching all unhandled exception types.
catch (Exception $e)
{
    echo $e->getMessage . " - " . $e->getCode();
}
# if you're using php 5, you can also use finally instruction
finally
{
}

您可以在PHP文档中找到一些关于异常的插入行,您还应该在这里查找默认的PHP异常类型。

我决定在Github上发布这篇文章:https://github.com/aws/aws-sdk-php/issues/111

得到的响应是,我应该检查所有S3调用的基本S3Exception