php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70030 try does not catch
Submitted: 2015-07-08 22:11 UTC Modified: 2015-07-08 23:45 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: bugzilla77 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.6.10 OS: Windows 7
Private report: No CVE-ID: None
 [2015-07-08 22:11 UTC] bugzilla77 at gmail dot com
Description:
------------
try does not catch

Test script:
---------------
<?php
 $d=new DOMDocument();
 $d->loadHTML('<html></html>');
 $p=new DOMXPath($d);

 try{
  $p->query('//html/head')->item(0)->getAttribute('rel');
 }catch(Exception $e){
  print('Catched');
 }
?>

Expected result:
----------------
Catched

Actual result:
--------------
Fatal error: Call to a member function getAttribute() on null in C:\htdocs\test.pl\1.php on line 11

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-08 23:45 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-07-08 23:45 UTC] requinix@php.net
As the error message clearly states, it's a fatal error. Not an exception. You cannot catch errors like that.
Do the responsible thing and check for valid objects before you try to call member methods.

With that said, PHP 7 is turning errors like this into exceptions so you will be able to catch them. However it will be an Error, which is not a descendant of Exception, so you would need something else in your catch block.
https://wiki.php.net/rfc/throwable-interface
http://3v4l.org/bhBLr (see 7.0.0alpha2)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 01 12:01:31 2024 UTC