0
好吧,我很困惑。我使用的是超薄框架,我有以下代码:PHP试试catch跳过catch,直接跳进
1 $app->post("/user/login", function (Request $request, Response $response) {
2 $resp = null;
3 try {
4 if (!array_key_exists("password", $request->getParams()) || !array_key_exists("username", $request->getParams())) {
5 throw new LiberInvalidRequestException("Not all required parameters were passed.");
6 }
7 $user = new User($this->db, [
8 'username' => $request->getParam('username'),
9 'password' => $request->getParam('password')
10 ]);
11 try {
12 $resp['logincode'] = $user->login();
13 $resp['success'] = true;
14 } catch (LiberAuthenticationException $exception) {
15 $resp['success'] = false;
16 $resp['errormessage'] = $exception->getMessage();
17 }
18 } catch (LiberInvalidRequestException $exception) {
19 $resp['success'] = false;
20 $resp['errormessage'] = $exception->getMessage();
21 } finally {
22 return $response->withJson($resp);
23 }
24 });
当我打电话用正确的PARAMS这条路,我收到了null
响应。在我看来,这应该是不可能的,因为在任何可能的方式中,东西都被分配给$resp
。当我用调试器通过这个函数时,我可以直到第12行,然后调试器跳转到finally块(第22行)。
这怎么可能?不应该继续执行还是转到catch块?
如果抛出了'LiberAuthenticationException'或'LiberInvalidRequestException'之外的其他异常,则可能发生命名空间问题? – deceze
@deceze我不认为会有另一个异常抛出。我发现当执行跳转到finally块时的Point是包含在composer自动加载器中的include语句。包含语句不会抛出Exception IIRC。 – Aaronmacaron
'try'内的'try'只会增加混淆。请参阅[this](http://stackoverflow.com/q/8439581/2298301)文章,了解如何为单个'try'写入多个'catch'块 - 如果需要的话。 –