PHP尝试捕捉一些问题
你好。是否可以在PHP中使用此类代码PHP尝试捕捉一些问题,php,try-catch,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 { //
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
块中捕获。您必须将整个内容嵌套在另一个外部try..catch
块中才能捕获这些内容 Nest multipletry…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
}
你试过了吗?你在找什么问题?是的。但也许尝试一下会比在这里提问快得多。您可以扩展exception类您面临的错误或问题是什么?为什么要这样做?->有时我有一个服务器错误,我需要在一个日志文件中写入,写入完成后,我想执行一个抛出到全局异常,该异常为用户设置并显示一个简单的用户消息,而不是技术消息。另一个问题是,我喜欢这个外观,但不太喜欢try-catch-multiple-level我想使用这种代码结构try{throw-new-InternalException('Internal');}catch(InternalException$e){throw-new-Exception('Internal-to-global');}catch(Exception$e){print$e->getMessage e();}