dongmi4809 2017-12-02 02: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 02: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条)
编辑
预览

报告相同问题?