dongmi4809 2017-12-02 10:01
浏览 35
已采纳

尝试在php pdo中捕获,正确的方式

I am using php pdo with postgres. I am using core php, no mvc, no classes and objects. I have some doubts in my mind since i am new to development, :-

  • Should i use try catch on every query execution.
  • And if i use try catch then should i exit the running script on exception

  • And is there a way to use one try catch for all queries, i mean making a common function for executing queries.

  • And how to handle exceptions on ajax.

please some one clear my doubts about this.

  • 写回答

2条回答 默认 最新

  • dongxuan2577 2017-12-02 10:54
    关注

    There are too many questions and they are too generic. But in most cases:

    1. No way
    2. it's up to you, your app's logic and particular case. Say if there is water leak somewhere in the city is it expected to stop water supply system completely? Typically there is concrete module/part that does not make sense to process further once DB query fails. And only that module/part should exit/stop.
    3. it's better not doing your own wrapping around PDO. in most cases you will just make code harder to maintain without any real benefit.
    4. typically the best solution is to return some HTTP error(404 Not Found or 500 Internal Server Error) letting client code to know "sorry, you cannot get this information yet"; how client code will handle such an error - it depends on your client code's logic/flow
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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