What are the best practices for handling error messages in PHP, especially when integrating external libraries like Smarty?

When handling error messages in PHP, especially when integrating external libraries like Smarty, it is important to properly catch and handle exceptions to prevent them from being displayed to users. One best practice is to use try-catch blocks to catch exceptions and then log or display appropriate error messages. Additionally, you can customize error handling by setting up custom error handlers to handle different types of errors.

try {
    // Code that may throw exceptions
    $smarty->display('template.tpl');
} catch (Exception $e) {
    // Log the error message
    error_log('Error: ' . $e->getMessage());
    
    // Display a user-friendly error message
    echo 'An error occurred. Please try again later.';
}