Junior — Middle
Have you used exception handling with try and catch blocks in your practice?
sobes.tech AI
Answer from AI
Yes, in PHP, exception handling using try and catch blocks is a standard way to manage errors, especially in object-oriented code.
Example of usage:
try {
$db = new PDO($dsn, $user, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Execute queries
} catch (PDOException $e) {
echo "Database connection error: " . $e->getMessage();
}
This approach allows catching exceptions, handling errors centrally, and preventing application crashes.