[英]PHP try-catch some problems
你好。 是否可以在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
}
是的,您可以分别捕获特定的异常。
仅当将异常抛出到try
块中时,才会捕获它们。 在catch
块中引发的异常不会在同一try..catch
语句的其他同级catch
块中catch
。 您必须将整个东西嵌套在另一个外部try..catch
块中才能捕获它们。
嵌套多个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
}
“转换”异常没有任何意义。 如果您不处理它们,请不要扔它们。
您可以通过以下方式捕获不同的异常:
try {
throw new InternalException();
} catch (HardwareException $e) {
} catch (InternalException $e) {
// this catch block will be executed
} catch (Exception $e) {
// all other exceptions
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.