PHP尝试捕获一些问题

时间:2013-09-25 08:28:04

标签: php try-catch

您好。是否可以在 PHP

中使用此类代码
try {
  throw new InternalException('Internal');
} catch (InternalException $e) {
  throw new Exception('Internal To global');
} catch (Exception $e){
  print $e->getMessage();
}

class InternalException extends Exception {
  // some code here
}

4 个答案:

答案 0 :(得分:0)

是的,您可以单独捕获特定的例外情况。

答案 1 :(得分:0)

只有在try块中抛出异常时才会触发异常。 catch块中抛出的异常在同一catch语句的其他同级try..catch块中捕获。你必须将整个东西嵌套在另一个外部try..catch块中以捕获它们。

答案 2 :(得分:0)

改为嵌套多个try...catch

try {
    throw new InternalException('Internal');
} catch (InternalException $e) {
    try {
        throw new Exception('Internal To global');
    } catch (Exception $e){
        print $e->getMessage();
    }
}

class InternalException extends Exception {
  // some code here
}

请参阅PHP: Exceptions - Manual

答案 3 :(得分:0)

“转换”异常没有意义。如果你不处理它们,不要扔它们。

您可以通过这种方式捕获不同的例外:

try {
    throw new InternalException();
} catch (HardwareException $e) {
} catch (InternalException $e) {
    // this catch block will be executed
} catch (Exception $e) {
    // all other exceptions
}