Skip to main content
PHP Bug Hunt Hard +35 XP

Missing Exception Handling

Database errors crash the application.

PHPFind the bug
1$pdo = new PDO($dsn, $user, $pass);
2$stmt = $pdo->query($sql);
The bug is on line 1

Select the correct fix

Show answer and explanation

Answer: Wrap in try/catch PDOException

PDO throws PDOException on errors. Wrapping in try/catch lets you handle DB errors gracefully instead of crashing.

Hint: PDO throws exceptions that must be caught.

What this challenge tests

Bug Hunt in PHP: exceptions, pdo.

Related challenges