doudou6050 2014-12-03 09:31
浏览 90
已采纳

PHP尝试和捕获或@

The question is in terms to don't allow app to throw exception and don't stop the application in not critical elements (expendable like log history of users search saved to DB) is it better to use this code:

try {
    /* some code here that will throw an exception at any error/miss-configuration because it is the 
       way this module works */

    $user = Sentry::getUser();
} catch (Exception $e) {}

or:

@$user = Sentry::getUser();

It's just an theoretical code but in fact Sentry is a good example to show what I mean. In this case if user is not logged in then Sentry will throw an exception.

As I said it's just about code that is not critical for application and I am not going to overuse.

Obviously better approach is to write code to prevent this kind of situation at all. Sometimes however it is necessary to stfu system :)

  • 写回答

1条回答 默认 最新

  • duanhuanbo5225 2014-12-03 09:36
    关注

    If the code throws an exception, the only way to handle that is with a try/catch block.

    @code only surpresses error messages; fatal errors and exceptions will still stop executing the code.

    Alternatively, if you have a lot of exceptions, you can try setting your own exception handler, but I can't say this is a good idea.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题