当我连接到DB时,我试图捕获php错误
我有一个类似
的东西 try{
//connect to DB
}catch(exception $e){
echo $e
}
//other php codes...
//My html elements...
<div>....
我的问题是,我想跳过//other phpo codes
,如果我们有错误连接到DB和直接显示我的html元素。这可能吗?非常感谢。
在您的try/catch
中取出代码。一旦抛出异常,执行将被移交给控制结构的catch
部分,并且永远不会到达该部分代码:
try{
//connect to DB
// If an exception is throw above we never get here
//other php codes...
}catch(exception $e){
echo $e
}
//My html elements...
<div>....
如果你不想移动//其他php代码如果你不想/不能编辑try/catch块,那么try/catch肯定会返回一些你可以测试的变量,即使只有$e.
try {
// something like $connected_db should be available
}
catch (exception $e)
{
}
if (!empty($connected_db) AND empty($e)) // one or the other depending on the code above
{
// other php code
}
// my html elements