0
0
PHPprogramming~10 mins

Error handling with PDO exceptions in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error handling with PDO exceptions
Start
Try to connect to DB
PDO throws exception?
NoUse DB connection
Yes
Catch exception
Handle error (e.g., show message)
End
The program tries to connect to the database. If an error happens, it catches the exception and handles it gracefully.
Execution Sample
PHP
<?php
try {
  $pdo = new PDO($dsn, $user, $pass);
  echo "Connected!";
} catch (PDOException $e) {
  echo "Error: " . $e->getMessage();
}
?>
This code tries to connect to a database and prints a message if it succeeds or an error message if it fails.
Execution Table
StepActionEvaluationResult
1Try to create PDO objectPDO constructor called with DSN, user, passThrows exception if connection fails, else creates object
2PDO connection success?No exception thrownProceed to echo 'Connected!'
3OutputPrint 'Connected!'User sees: Connected!
4Try to create PDO objectPDO constructor called with wrong DSNThrows PDOException
5Catch PDOExceptionCatch block runsPrint error message with exception text
6OutputPrint error messageUser sees: Error: [exception message]
💡 Execution stops after printing success or error message.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
$pdonullPDO object if successnull (exception thrown)PDO object or null
$eundefinedundefinedPDOException objectPDOException object or undefined
Key Moments - 3 Insights
Why do we use try-catch with PDO?
Because PDO throws exceptions on errors, try-catch lets us handle these errors without stopping the whole program. See execution_table rows 1 and 4.
What happens if the connection is successful?
The code inside try runs fully and prints 'Connected!'. No catch block runs. See execution_table rows 2 and 3.
What if the connection fails?
PDO throws an exception, the catch block runs and prints the error message. See execution_table rows 4, 5, and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what variable holds the PDOException object?
A$pdo
B$e
C$dsn
D$user
💡 Hint
Check variable_tracker row for $e after Step 4
At which step does the program print 'Connected!'?
AStep 3
BStep 1
CStep 5
DStep 6
💡 Hint
Look at execution_table row with Output 'Connected!'
If the DSN is wrong, what happens in the try block?
ANothing happens
BPDO object is created successfully
CException is thrown and caught
DProgram crashes without message
💡 Hint
See execution_table rows 4 and 5 about exception throwing and catching
Concept Snapshot
try {
  $pdo = new PDO($dsn, $user, $pass); // try to connect
  echo "Connected!"; // success message
} catch (PDOException $e) {
  echo "Error: " . $e->getMessage(); // handle error
}
Use try-catch to catch PDO exceptions and avoid program crash.
Full Transcript
This example shows how to handle errors when connecting to a database using PDO in PHP. The code tries to create a PDO object inside a try block. If the connection is successful, it prints 'Connected!'. If there is a problem, PDO throws an exception. The catch block catches this exception and prints an error message. This way, the program does not crash and shows a friendly message instead.