How can error handling in PHP scripts be improved to prevent unexpected behavior?

Error handling in PHP scripts can be improved by using try-catch blocks to catch exceptions and handle them gracefully. This helps prevent unexpected behavior and allows for more controlled handling of errors.

try {
    // Code that may throw an exception
    $result = some_function();
} catch (Exception $e) {
    // Handle the exception
    echo 'An error occurred: ' . $e->getMessage();
}