Should we use try catch to prompt the user or should we use if(){echo 'success';}else{echo 'failure';}
And what is the difference between the two?
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.
黄舟2017-05-16 13:14:18
If is for branching, try catch is for catching exceptions thrown in the code.
PHP中文网2017-05-16 13:14:18
if
和try catch
It 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
,而我们没有必要自己再做这种事情,而且过多的使用if
Doing anomaly detection will inevitably make people feel weird
滿天的星座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!
迷茫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
仅有的幸福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
伊谢尔伦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
给我你的怀抱2017-05-16 13:14:18
if is generally used to handle known errors, and try is used to handle unknown errors.