How can PHP beginners effectively handle error handling when checking for text on a webpage?

When checking for text on a webpage in PHP, beginners can effectively handle error handling by using try-catch blocks to catch any exceptions that may occur during the execution of the code. This allows for graceful error handling and prevents the script from crashing if an error occurs.

try {
    $html = file_get_contents('http://www.example.com');
    if($html === false){
        throw new Exception('Failed to fetch webpage');
    }
    
    $text = 'Hello World';
    
    if(strpos($html, $text) !== false){
        echo 'Text found on webpage';
    } else {
        echo 'Text not found on webpage';
    }
} catch(Exception $e) {
    echo 'Error: ' . $e->getMessage();
}