mrt*_*man 7 php exception-handling try-catch
我试图将异常从特定的catch块传递给更通用的catch块.但它似乎没有起作用.我尝试以下操作时出现500服务器错误.这甚至可能吗?
我意识到有一些简单的解决方法,但是说"嘿,我不想处理这个错误,让我们有更普遍的异常处理程序来处理它是不正常的!"
try {
//some soap stuff
}
catch (SoapFault $sf) {
throw new Exception('Soap Fault');
}
catch (Exception $e) {
echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
从技术上讲,这就是你要找的东西:
try {
try {
//some soap stuff
}
catch (SoapFault $sf) {
throw new Exception('Soap Fault');
}
}
catch (Exception $e) {
echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
但我同意不应将例外用于流量控制.更好的方法是这样的:
function show_error($message) {
echo "Error: $message\n";
}
try {
//some soap stuff
}
catch (SoapFault $sf) {
show_error('Soap Fault');
}
catch (Exception $e) {
show_error($e->getMessage());
}
Run Code Online (Sandbox Code Playgroud)