try {
// statements that might throw an exception
} catch (ExceptionClass $exception_name) {
// statements that handle the exception
}
$objectName->methodName(argumentList);
try {
$db = new PDO($dsn, $username, $password);
echo("You are connected to the database!</p>\n");
} catch (PDOException $e) {
$error_message = $e->getMessage();
echo("<p>An error occurred while connecting to the database: $error_message&;t'/p>");
exit;
}
try {
// statements that might throw an exception
} catch (Exception $e) {
$error_message = $e->getMessage();
echo(<p>An error occurred while connecting to the database: $error_message);
exit;
}
try/catch statement. First, you code a try block around any PHP statements that might throw an exception. Then, you code a catch block that catches the exception. This is known as exception handling.->, followed by the name of the method, followed by a set of parentheses. Within the parentheses, you code the arguments that are required by the method, separating multiple arguments with commas.getMessage() method that lets you get the error message.