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条)

报告相同问题?

悬赏问题

  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题