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.

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

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式