PHP异常内部捕获:如何处理? [英] PHP exception inside catch: how to handle it?

查看:84
本文介绍了PHP异常内部捕获:如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在try...catch块中包含一个PHP代码.假设在catch内部,您想做一些可能会失败并引发新异常的事情(即发送电子邮件).

Suppose to have a PHP code inside a try...catch block. Suppose that inside catch you would like to do something (i.e. sending email) that could potentially fail and throw a new exception.

try {
    // something bad happens
    throw new Exception('Exception 1');
}
catch(Exception $e) {
    // something bad happens also here
    throw new Exception('Exception 2');
}

catch块中处理异常的正确(最佳)方法是什么?

What is the correct (best) way to handle exceptions inside catch block?

推荐答案

基于此答案,它似乎是嵌套try/catch块非常有效,就像这样:

Based on this answer, it seems to be perfectly valid to nest try/catch blocks, like this:

try {
   // Dangerous operation
} catch (Exception $e) {
   try {
      // Send notification email of failure
   } catch (Exception $e) {
      // Ouch, email failed too
   }
}

这篇关于PHP异常内部捕获:如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆