本文介绍了最好的做法是尝试 - 抓住我的整个PHP代码,或尽可能具体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中没有很多种例外。

现在,(我们使用MVC)我的try catch包含了我的整个代码:

  try {
fronController :: dispatch($ somthing ...);
} catch(异常$ E){
//处理错误
}


$ b $我想知道是否有一个很好的理由尽可能地使用try-catch块,尽可能地像现在那样保持一般性。

解决方案

通常在本地抛出,捕获全局,除非异常处理程序是特定于一个函数,在这种情况下本地处理。

  class fooException extends Exception {} 

// DB CLASS

public function Open(){
//打开DB连接
...
if($ this-> Conn-> connect_errno)
throw new fooException(Could not connect:$ this-> Conn-> connect_error);
}

// MAIN CLASS

public final function Main(){
try {
// do stuff
}
catch(fooException $ ex){
// handle fooExceptions
}
}


I do not have many kinds of Exceptions in my project.
Right now,(we use MVC) I have the try catch encompassing my entire code:

try{
   fronController::dispatch($somthing...);
}catch(Exception $E){
  //handle errors
}

I wonder if there is a good reason to use the try-catch block in as specific as possible way as I can or just keep it general as it is now?

解决方案

generally throw locally, catch globally unless an exception handler is specific to a function in which case handle locally.

 class fooException extends Exception{}

 // DB CLASS

 public function Open(){
    // open DB connection
    ...
    if ($this->Conn->connect_errno) 
      throw new fooException("Could not connect: " . $this->Conn->connect_error);
  }

 // MAIN CLASS

 public final function Main(){
    try{
      // do stuff
    }
    catch(fooException $ex){
       //handle fooExceptions
    }
 }

这篇关于最好的做法是尝试 - 抓住我的整个PHP代码,或尽可能具体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:09