为什么会出现此错误?
Warning: file_get_contents(http://www.example.com) [function.file-get-contents]: failed to open stream: HTTP request failed! in C:\xampp\htdocs\test.php on line 22
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\test.php on line 22
这是代码:
try {
$sgs = file_get_contents("http://www.example.com");
}
catch (Exception $e) {
echo '123';
}
echo '467';
不是不应该\ catch继续执行代码?也许有其他不同的方法可以做到?
最佳答案
尝试...对于空对象异常和手动引发的异常,捕获更多。确实,这与您在Java中可能看到的范式不同。警告几乎具有欺骗性,因为它们将特别忽略try ... catch块。
要禁止显示警告,请在方法调用(或数组访问)前添加@
。
$a = array();
$b = @$a[ 1 ]; // array key does not exist, but there is no error.
$foo = @file_get_contents( "http://somewhere.com" );
if( FALSE === $foo ){
// you may want to read on === there;s a lot to cover here.
// read has failed.
}
哦,最好是查看致命异常也是完全不可捕获的。在某些情况下可能会捕获其中的一些,但实际上,您想修复致命错误,而您不想处理它们。