php try ... else [英] php try ... else
本文介绍了php try ... else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在PHP中使用类似的东西到 try ... else
知道try块是否正确执行,当块正确执行时,将打印一条消息。
解决方案
您可以使用 try {} catch(){}
和 throw
。请参阅 http://php.net/manual/en/language.exceptions.php
try {
$ a = 13/0; //应该抛出异常
} catch(异常$ e){
echo'捕获异常:',$ e-> getMessage(),\\\
;
}
或手动:
try {
throw new Exception(我不想尝试!);
} catch(Exception $ e){
echo'捕获异常:',$ e-> getMessage(),\\\
;
}
Is there something similar in PHP to the try ... else
in Python?
I need to know if the try block executed correctly as when the block executed correctly, a message will be printed.
解决方案
You can use try { } catch () { }
and throw
. See http://php.net/manual/en/language.exceptions.php
try {
$a = 13/0; // should throw exception
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
or manually:
try {
throw new Exception("I don't want to be tried!");
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
这篇关于php try ... else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文