Exceptions in PHP

From the PHP cheat sheet · Error Handling · verified Jul 2026

Exceptions

Working with exceptions and try-catch blocks

php
// Basic exception handling
try {
    $result = riskyOperation();
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

// Multiple catch blocks
try {
    $data = processData();
} catch (InvalidArgumentException $e) {
    // Handle invalid argument
} catch (RuntimeException $e) {
    // Handle runtime error
} finally {
    // Always executes
    cleanup();
}
💡 Use specific exception types for different errors
⚡ Finally block always executes for cleanup
📌 Chain exceptions to preserve error context
🔥 Throwable catches both Exception and Error types
Back to the full PHP cheat sheet