您好。是否可以在 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
}
答案 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
}
答案 3 :(得分:0)
“转换”异常没有意义。如果你不处理它们,不要扔它们。
您可以通过这种方式捕获不同的例外:
try {
throw new InternalException();
} catch (HardwareException $e) {
} catch (InternalException $e) {
// this catch block will be executed
} catch (Exception $e) {
// all other exceptions
}