2019-03-25 12:46
浏览 88


So I have this little bit of code

public function getPrices($debtorId)
    $priceListId = $this->getPriceListId($debtorId);
        throw new \Exception('No list found for this customer');

    // doing some operations here that require $priceListId

    return $prices;

Up until now, I would have done something like

if(!$priceListId) exit('No list found for this customer');

The difference being, that I could catch the Exception if I want to, while that's not possible with the exit statement.

However, in this case I do want my program to exit. But my IDE warns me that I'm not catching Exceptions. So, should I really do this now:

try {
    $prices = $priceHandler->getPrices($debtorId);
} catch(Exception $e) {

The latter appears to me as unecessary and actually decreasing code quality. So: Is it acceptable to deliberately NOT catch some exceptions? Or should I even get rid of the exception alltogether and just use plain old exit?

I tried searching for this question, but I only got results about people who had technical problems with try/catch not working.

图片转代码服务由CSDN问答提供 功能建议


 公共函数 getPrices($ debtorId)
 $ priceListId = $ this-> getPriceListId($ debtorId); 
 if(!$ priceListId){
 throw new \ Exception('找不到此客户的列表');  
 //在这里做一些需要$ priceListId 
返回$ price的操作; 

到目前为止,我会 做了类似

  if(!$ priceListId)exit('找不到该客户的列表'); 


但是,在这种情况下 我确实希望我的程序退出。 但我的IDE警告我,我没有捕捉异常。 所以,我现在应该真的这样做:

 $ prices = $ priceHandler-> getPrices($ debtorId); 
} catch(异常$ e)  {
 exit($ e-> getMessage()); 

后者在我看来是不必要的,实际上降低了代码质量。 所以:故意不捕捉一些例外是否可以接受?或者我是否应该完全摆脱异常而只使用普通的旧退出 \ n

我试着搜索这个问题,但是我只得到了那些遇到技术问题而且try / catch无法正常工作的结果。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题