0
0
PHPprogramming~20 mins

Error handling with PDO exceptions in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PDO Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when a PDO exception occurs?
Consider the following PHP code that tries to connect to a database with wrong credentials. What will be the output?
PHP
<?php
try {
  $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'wronguser', 'wrongpass');
  echo "Connected successfully";
} catch (PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}
?>
AConnected successfully
BFatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'wronguser'@'localhost' (using password: YES)
CConnection failed: SQLSTATE[HY000] [1045] Access denied for user 'wronguser'@'localhost' (using password: YES)
DNo output
Attempts:
2 left
💡 Hint
Think about what happens when the connection fails and how exceptions are caught.
Predict Output
intermediate
2:00remaining
What error is raised when accessing a non-existent PDO attribute?
What error will the following PHP code produce?
PHP
<?php
$pdo = new PDO('sqlite::memory:');
echo $pdo->getAttribute(PDO::ATTR_NON_EXISTENT);
?>
ANULL
BWarning: PDO::getAttribute() expects parameter 1 to be integer, string given
CNotice: Undefined class constant 'ATTR_NON_EXISTENT'
DFatal error: Uncaught Error: Undefined class constant 'ATTR_NON_EXISTENT'
Attempts:
2 left
💡 Hint
Check if the constant exists in the PDO class.
🔧 Debug
advanced
2:00remaining
Why does this PDO exception not get caught?
Examine the code below. Why does the exception not get caught by the catch block?
PHP
<?php
try {
  $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'wronguser', 'wrongpass');
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $pdo->query('INVALID SQL');
} catch (PDOException $e) {
  echo "Error caught: " . $e->getMessage();
}
?>
AThe exception is not caught because the error mode is set after the connection attempt.
BThe exception is not caught because query() does not throw exceptions.
CThe exception is caught and the message is printed.
DThe code has a syntax error and does not run.
Attempts:
2 left
💡 Hint
Think about when the error mode is set and when exceptions are thrown.
Predict Output
advanced
2:00remaining
What is the output of this PDO exception handling code?
What will this PHP code output?
PHP
<?php
try {
  $pdo = new PDO('sqlite::memory:');
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $pdo->exec('CREATE TABLE test(id INTEGER PRIMARY KEY)');
  $pdo->exec('INSERT INTO test(id) VALUES(1)');
  $pdo->exec('INSERT INTO test(id) VALUES(1)');
  echo "Insert succeeded";
} catch (PDOException $e) {
  echo "Insert failed: " . $e->getMessage();
}
?>
AInsert failed: SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: test.id
BNo output
CFatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation
DInsert succeeded
Attempts:
2 left
💡 Hint
Think about what happens when you insert a duplicate primary key with exceptions enabled.
🧠 Conceptual
expert
3:00remaining
Which option correctly demonstrates catching and rethrowing a PDO exception?
You want to catch a PDOException, log the error message, and then rethrow it to be handled elsewhere. Which code snippet does this correctly?
A
&lt;?php
try {
  $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
} catch (PDOException $e) {
  throw $e;
  error_log($e-&gt;getMessage());
}
?&gt;
B
&lt;?php
try {
  $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
} catch (PDOException $e) {
  error_log($e-&gt;getMessage());
  throw $e;
}
?&gt;
C
&gt;?
}
;e$ worht  
;))(egasseMteg&gt;-e$(gol_rorre  
{ )e$ noitpecxEODP( hctac }
;)'ssap' ,'resu' ,'bdtset=emanbd;tsohlacol=tsoh:lqsym'(ODP wen = odp$  
{ yrt
php?&lt;
D
&lt;?php
try {
  $pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
} catch (PDOException $e) {
  error_log($e-&gt;getMessage());
}
throw $e;
?&gt;
Attempts:
2 left
💡 Hint
Remember that code after throw is not executed and variables must be in scope.