is class exception parent of class xmlexception extends exception, and when does code break

Posted on 2014-04-23
Last Modified: 2014-04-28

class XmlException extends Exception {
    private $error;

    function __construct( LibXmlError $error ) {
        $shortfile = basename( $error->file );
	print_r( $error );
        $msg = "[{$shortfile}, line# {$error->line}, col# {$error->column}] {$error->message}";
        $this->error = $error;
        parent::__construct( $msg, $error->code );

    function getLibXmlError() {
        return $this->error;

class FileException extends Exception { }
class ConfException extends Exception { }

class Conf {
    private $file;
    private $xml;
    private $lastmatch;

    function __construct( $file ) {
        $this->file = $file;
        if ( ! file_exists( $file ) ) {
            throw new FileException( "file '$file' does not exist" );
        $this->xml = simplexml_load_file($file, null, LIBXML_NOERROR );
        if ( ! is_object( $this->xml ) ) {
            throw new XmlException( libxml_get_last_error() );
        $matches = $this->xml->xpath("/conf");
        if ( ! count( $matches ) ) {
            throw new ConfException( "could not find root element: conf" );

    function write() {
        if ( ! is_writeable( $this->file ) ) {
            throw new Exception("file '{$this->file}' is not writeable");
        file_put_contents( $this->file, $this->xml->asXML() );

    function get( $str ) {
        $matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");
        if ( count( $matches ) ) {
            $this->lastmatch = $matches[0];
            return (string)$matches[0];
        return null;

    function set( $key, $value ) {
        if ( ! is_null( $this->get( $key ) ) ) {
        $conf = $this->xml->conf;
        $this->xml->addChild('item', $value)->addAttribute( 'name', $key );

class Runner {
    static function init() { 
        try {
            //$error_test= new XmlException(1);
            //$conf = new Conf( dirname(__FILE__)."/conf01-edit.xml" );
            $conf = new Conf( dirname(__FILE__)."/conf.broken.xml" );
            $conf = new Conf( dirname(__FILE__)."/fake.xml" );
            print "user: ".$conf->get('user')."\n";
            print "host: ".$conf->get('host')."\n";
            $conf->set("pass", "newpass");
        } catch ( FileException $e ) {
            // permissions issue or non-existent file
            throw $e;
        } catch ( XmlException $e ) {
            // broken xml
        } catch ( ConfException $e ) {
            // wrong kind of XML file
        } catch ( Exception $e ) {
            // backstop: should not be called



Open in new window

LibXMLError Object ( [level] => 3 [code
] => 76 [column] => 12 [message] => Opening and ending tag mismatch: conf-broked line 2 and conf [file] => file:///C:/wamp/www/POPP-edition4-code/9781430260318_Chapter_04_Code/conf.broken.xml [line] => 6 )

when does this code break?
does code break after running
parent::__construct( $msg, $error->code );
is class exception parent of class xmlexception extends exception
after running line11
debugger goes to line12 and breaks
I think I know why error
why did code stop at that line
Question by:rgb192
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
LVL 57

Expert Comment

by:Julian Hansen
ID: 40019407
Code is breaking on line 76
LibXMLError Object ( [level] => 3 [code
] => 76 [column] => 12 

Open in new window

Error is caused by a problem in the xml file you are using.
LVL 110

Expert Comment

by:Ray Paseur
ID: 40019879
is class exception parent of class xmlexception extends exception
why did code stop at that line
Line 74 creates a new Conf object in the variable named $conf.  Line 75 overwrites variable $conf with another new Conf object.  Line 76 does not appear to be the problem (I think the "76" came from an error code, not a line number).  I believe line 34 threw a new XmlException using the data in the PHP function libxml_get_last_error().  This happened because of the creation of a new Conf object on line 74, where the XML document is not valid.  From the document name, it appears that the XML is intentionally not valid.

Executive summary: The code did not "break" at all.  Line 11 called the Exception constructor.  This was perfectly normal and expected.

Maybe it's the IDE that has trouble following the logic here?

Author Comment

ID: 40021606
line 11
 parent::__construct( $msg, $error->code );

is run

is this the 'error class'  which is not visible by my ide

ide waits 10 seconds and then runs line 12

and then stops

maybe finished the 'error child' class
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 110

Expert Comment

by:Ray Paseur
ID: 40021728
I don't know.  When you have layers of IDEs and example code, anyone's guess is as good as mine.  Anything that waits 10 seconds then resumes is a strange design.

Author Comment

ID: 40021776
is class exception parent of class xmlexception extends exception
LVL 57

Accepted Solution

Julian Hansen earned 500 total points
ID: 40021970
Did not read your question properly before.

This is what is happening
- The code is running to line 74.
- The code attempts to read the broken xml file on line 32.
- On line 33 the return from 32 is tested to see if it is valid but fails
- The code drops to line 34 and throws an XmlException passing in the last error encountered by lib XML.
- This results in the instantiation of the XmlException object - and where your print_r($error) line executes
- The code then drops into the catch statement on line 84 - at this point you could do the following to your catch block
        } catch ( XmlException $e ) {
           $error = $e->getLibXmlError();
           echo "Exception caught: " . $error->message . 'in file : ' . $error->file . ' on line ' . $error->line ;
            // broken xml

Open in new window

You can also make your error dump more informative by adding <pre> tags around it i.e.
echo "<pre>";
echo "</pre>";

Open in new window

Which should output something like this
LibXMLError Object
    [level] => 3
    [code] => 5
    [column] => 56
    [message] => Extra content at the end of the document

    [file] => file:///G:/Projects/Sandbox/website/ee/broken.xml
    [line] => 6

Open in new window

The reason your debug stops on line 12 is that the exception is instantiated but you don't do anything with it in the catch block so there is nothing more to do.

By adding the code above to the catch block you should see the code terminate on or around line 87

Author Closing Comment

ID: 40028560
thanks for information to dump variable values.
LVL 57

Expert Comment

by:Julian Hansen
ID: 40028834
You are welcome - thanks for the points.

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question