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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bugzilla77 at gmail dot com
New email:
PHP Version: OS:

 

 [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 14:01:29 2024 UTC