How can exceptions and try-catch blocks be used in PHP to handle errors more effectively?

Exceptions and try-catch blocks can be used in PHP to handle errors more effectively by allowing you to catch and handle specific types of errors or exceptions that may occur during the execution of your code. This can help you gracefully handle errors without causing your program to crash or display error messages to users.

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