在 MVC 框架中捕获异常的位置

Where to catch exceptions in a MVC framework

每个人都在谈论异常处理的重要性。直到最近我才需要处理它们:

try { 
    $pdo = new PDO($dns); 
} catch (Exception $e) {
    throw new Exception($e); 
} 

另一个更一般的例子是:

if ($something) {
    throw new Exception('Ouch! I can't do that!');
}

网上查了一下,发现网上很多examples/solutions只是附和一下。 (PDO Exception Questions - How to Catch Them) Some don't have solutions. (Catching PDOException in lower layer and re-throwing as different exception to upper layer) & () And others, I just dont understand. (Where to catch exceptions)

问题 #1:我应该在哪里捕获异常;在 MVCish 框架的模型或控制器中?

问题#2:捕捉它应该做什么?登录,通过电子邮件发送,显示 503?

我是 .net 专家,对我来说捕获异常的最佳实践:

  • 越晚越好。
  • 编写一个通用代码来捕获异常。
  • 在控制器级别捕获它并用完整的堆栈跟踪(ETW、日志文件、数据库等)将其记录在某个地方。
  • 将用户重定向到一些好看的错误页面。只有控制器可以执行此操作。