tags:

views:

31

answers:

1
+1  Q: 

PHP handle errors

Using this code with simplehtmldom script (http://simplehtmldom.sourceforge.net/manual.htm):

function file_get_html() {
    $dom = new simple_html_dom;
    $args = func_get_args();
    $dom->load(call_user_func_array('file_get_contents', $args), true);
    return $dom;
}

$url = 'http://site.com/';
$html = file_get_html($url);

How to handle erros on file_get_html($url) part? Now, if page doesn't exist, it shows errors in browser window. I prefer to catch them and show my text instead, like:

if(some error happened on file_get_html($url)) {
   $errors = true;
} else {
   html = file_get_html($url);
}

Thanks.

+2  A: 
Sarfraz
please tell a bit more about catch() part. $e->getMessage() - what does it mean?
Happy
seems it doesn't work, gives an error (like before): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
Happy
I think Simple HTML DOM Parser does not throw any exceptions.
Gumbo
@Ignatz: See this please for more info: http://php.net/manual/en/language.exceptions.php
Sarfraz
@Ignatz: See my updated answer please.
Sarfraz