我遇到过这个: –
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(); }