php – 如何在mysql数据库连接中使用throw异常

前端之家收集整理的这篇文章主要介绍了php – 如何在mysql数据库连接中使用throw异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到过这个: –

PHP Error handling: die() Vs trigger_error() Vs throw Exception

并且理解抛出异常更好

如何在此代码中替换die并使用throw异常: –

<?PHP
# FileName="Connection_PHP_MysqL.htm"
# Type="MysqL"
# HTTP="true"
$hostname_db = "localhost";
$database_db = "database";
$username_db = "root";
$password_db = "password";
$db = MysqLi_connect($hostname_db,$username_db,$password_db) or die("Unable to connect with Database"); 
?>
try
{
    if ($db = MysqLi_connect($hostname_db,$password_db))
    {
        //do something
    }
    else
    {
        throw new Exception('Unable to connect');
    }
}
catch(Exception $e)
{
    echo $e->getMessage();
}

猜你在找的PHP相关文章