Sch*_*ute 8 php exception-handling google-api-php-client
我想捕获Google API PHP库引发的异常,但由于某种原因,它会在到达catch块之前生成" 致命错误:未捕获的异常 ".
在我的应用程序中,我有这样的事情:
try {
$google_client->authenticate($auth_code);
} catch (Exception $e) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
这是Google_Client的authenticate():
public function authenticate($code)
{
$this->authenticated = true;
return $this->getAuth()->authenticate($code);
}
Run Code Online (Sandbox Code Playgroud)
的authenticate($code)上面是Google_Auth_OAuth2 :: authenticate()的,其中在某一点抛出异常:
throw new Google_Auth_Exception(
sprintf(
"Error fetching OAuth2 access token, message: '%s'",
$decodedResponse
),
$response->getResponseHttpCode()
);
Run Code Online (Sandbox Code Playgroud)
如果我在Google_Client的身份验证中放置一个try/catch块,它会捕获异常,但如果没有它,程序就会死掉而不是从我的应用程序到达主try/catch块.
据我所知,这不应该发生.有任何想法吗?
Sch*_*ute 24
问题是try/catch块在命名空间文件中,PHP要求你使用"\ Exception".更多信息:PHP 5.3命名空间/异常陷阱
示例(取自上面的链接):
<?php
namespace test;
class Foo {
public function test() {
try {
something_that_might_break();
} catch (\Exception $e) { // <<<<<<<<<<< You must use the backslash
// something
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)