php - Is try catch for programmers or users
ringa_lee
ringa_lee 2017-05-16 13:12:18
0
9
510

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?

ringa_lee
ringa_lee

ringa_lee

reply all(9)
Ty80

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.

黄舟

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

洪涛

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

滿天的星座

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!

迷茫

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

仅有的幸福

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

伊谢尔伦

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

PHPzhong

if cannot span functions, but try can

给我你的怀抱

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template