Home  >  Q&A  >  body text

php - try catch是针对程序员还是用户的

给用户提示能用try catch吗还是要用if(){echo '成功';}else{echo '失败';}
而这两种又有何区别

ringa_leeringa_lee2712 days ago481

reply all(9)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:14:18

    try, like if, is a syntax provided by a programming language. There is no saying which one is for programmers and which one is for users.

    But they have many characteristics that are different. There is no iron-clad rule on which one to use.

    The title asks this question, which shows that the essence of using the abnormal mechanism has not been grasped. In this case, it is better to use if, it is simple and will not go wrong.

    reply
    0
  • 黄舟

    黄舟2017-05-16 13:14:18

    If is for branching, try catch is for catching exceptions thrown in the code.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:14:18

    iftry catchIt doesn’t say who it should be shown to, it depends on the situation. It is designed for business.

    if:一般用当前功能的逻辑上面的判断,判断一些可以预测的可能性;
    try catch:通常用于异常的捕捉,这些异常出现的可能性是不可预测的,比如‘磁盘内存不足’,‘0被整除’等等,为什么这里不用if呢?因为这些通用的异常一般不是被语言库包装成了对应的异常对象,就是在项目内部被封装成自己的库,然后可以广泛使用,当然了,用if也可以达到同样的效果,甚至源码里面都用到了if,而我们没有必要自己再做这种事情,而且过多的使用ifDoing anomaly detection will inevitably make people feel weird

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:14:18

    try catch is more used to catch unknown exceptions; if it is a foreseeable exception, try catch is also a good use to prompt the user; you can also use if to prompt information; if it is an unknown exception and you want to avoid exception prompts, try catch is a good idea!

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:14:18

    try catch is used to handle exceptions, suppress errors and collect error information. When if is true, the code is executed, else executes other code, and prompts the user to use if. There is no need to use try catch

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 13:14:18

    Of course it is for programmers...
    Which user will see the content of your echo...
    try catch is exception catching

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:14:18

    try catch is used to catch exceptions
    If you catch an exception, you can see the error log on the console, which is easy to view error information and debug.
    Catching exceptions. After throwing an exception, you can avoid the program from running in the event of an error. Crash

    reply
    0
  • PHPz

    PHPz2017-05-16 13:14:18

    if cannot span functions, but try can

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:14:18

    if is generally used to handle known errors, and try is used to handle unknown errors.

    reply
    0
  • Cancelreply