How can exceptions be caught and handled effectively in PHP applications to maintain code integrity?

When exceptions occur in PHP applications, they can disrupt the flow of the program and potentially lead to unexpected behavior. To maintain code integrity, exceptions should be caught and handled effectively using try-catch blocks. By encapsulating potentially error-prone code within a try block and providing specific error handling within a catch block, developers can gracefully manage exceptions and prevent them from causing critical issues.

try {
    // Potentially error-prone code here
    throw new Exception("An error occurred.");
} catch (Exception $e) {
    echo "Caught exception: " . $e->getMessage();
}