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

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合