What are the best practices for handling errors in separate methods within a PHP script?

When handling errors in separate methods within a PHP script, it is important to use try-catch blocks to catch exceptions and handle errors gracefully. This helps in isolating the error handling logic from the main code, making the script more maintainable and readable. Additionally, logging errors to a file or database can help in debugging and monitoring the application.

function method1() {
    try {
        // code that may throw an exception
    } catch (Exception $e) {
        // handle the error gracefully
        echo 'Error: ' . $e->getMessage();
        // log the error to a file or database
        error_log('Error: ' . $e->getMessage(), 3, 'error.log');
    }
}

function method2() {
    try {
        // code that may throw an exception
    } catch (Exception $e) {
        // handle the error gracefully
        echo 'Error: ' . $e->getMessage();
        // log the error to a file or database
        error_log('Error: ' . $e->getMessage(), 3, 'error.log');
    }
}

// call the methods
method1();
method2();