这个问题已经在这里有了答案 :
将查询错误转换为MySQLi中的异常 (3个答案)
9个月前关闭。
我碰到了这个:
PHP错误处理:die()VS trigger_error()VS throw异常
并且理解抛出异常更好
我如何在此代码中替换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, $username_db, $password_db)) { //do something } else { throw new Exception('Unable to connect'); } } catch(Exception $e) { echo $e->getMessage(); }