I am trying to handle exceptions in a more graceful way so that I can handle my own checks for issues as well as capturing PHP native errors.

Here is my testing code.

$filename = 'X1000000.xml';
$reader = new XMLReader();
try {
	if(!$reader->open($filename)){ 
		throw new Exception("can't open file");
	}

while ($fail==false) {
	if($reader->read()==false) {
		throw new Exception("parser error");
		$fail=true;
	}
}

} catch(Exception $e) {

echo "<pre>";
print_r(error_get_last());
echo "</pre>";


echo "<pre>";
print_r($e);
echo "</pre>";

echo 'Message: '   .$e->getMessage().'<br />';
}

My biggest issue is how to handle the while loop in this instance.
Normal coding for this block would be

while ($reader->read()) {

}

but if the error Occurs IN $reader->read() then how can I handle catching the error?
so i tried the code you see with while ($fail==false) but unfortunatly once it reaches the end of the data it returns false as well so i would get an exception in any case.

Can some tell me how you would normally handle an issue like this?
THANKS FOR ANY HELP!!

    rayge99;10988929 wrote:

    if the error Occurs IN $reader->read() then how can I handle catching the error?

    You'd do it just like you would any other error.

    If the read() function doesn't catch exceptions that it generates, then PHP will start travelling up the call stack in search of a try/catch block. If it reaches the top-most call and it still hasn't found a try/catch, that's when you see the "Unhandled exception..." messages.

      EXAMPLE??
      If im running the following PROPER code:

      $filename = 'X1000000.xml';
      $reader = new XMLReader();
      try {
      	if(!$reader->open($filename)){ 
      		throw new Exception("can't open file");
      	}
      
      while ($reader->read()) {
      }
      
      } catch(Exception $e) {
      
      echo "<pre>";
      print_r(error_get_last());
      echo "</pre>";
      
      
      echo "<pre>";
      print_r($e);
      echo "</pre>";
      
      echo 'Message: '   .$e->getMessage().'<br />';
      }

      how can i catch an exception in $reader->read()?
      right now with the code AS IS i get a Wanring printed to the screen but no exception captured..

      Thanks again for any help!

        rayge99;10988942 wrote:

        right now with the code AS IS i get a Wanring printed to the screen but no exception captured..

        Well then that means that there is no exception to capture.

          OK well the how can i handle this warning in such a way so that it doesn't dump a warning to the screen but i can capture it and print it out how I would like?
          Especially if i cant capture it in this method?

            I didnt think that mattered.

            Im testing this code on xml that was malformed and instead of capturing the error with other code I had and posting it to the screen for another developer to view it
            just continued saying everything was OK yet showing a warning on the screen.

            Here are the errors:
            Warning: XMLReader::read() [xmlreader.read]: file: {FILEPATH}.xml:525: parser error : StartTag: invalid element name in {FILEPATH}.php on line 10
            Warning: XMLReader::read() [xmlreader.read]: , Alarm_Img, Alarm_Snd FROM device_personal_reminders WHERE (reminder_end_date < in {FILEPATH}.php on line 10
            Warning: XMLReader::read() [xmlreader.read]: ^ in {FILEPATH}.php on line 10
            Warning: XMLReader::read() [xmlreader.read]: An Error Occured while reading in {FILEPATH}.php on line 10

            What i need to do is handle any native php warnings such as this in a way so i can control the output of them how i see fit.

              OK so since its not an exception its an error then it LOOKS LIKE i can use
              set_error_handler()

                I'm not sure what to say... it's frustrating, but there are times when I run into situations in PHP where there's no accessor-type function that I can use to check if the next action will cause an error, and yet there is no way to (gracefully/quietly) 'catch' any errors that due actually occur when you actually perform that action. Unless I'm missing something, it looks like you might be facing a similar dilemma here. One possible solution might be to: store current error logging/outputting settings somewhere, [man]ini_set/man them such that no PHP warnings are logged/displayed anywhere, perform whatever operation(s) you need, and then restore those previous settings after you're done.

                Or, alternatively, see below.

                rayge99;10988947 wrote:

                What i need to do is handle any native php warnings such as this in a way so i can control the output of them how i see fit.

                Have you considered writing your own error handler? (See the manual page for [man]set_error_handler/man for more info on that.)

                  LOL Good timing on that message

                    It half ass works for me.. NOW the question is how to output the data from the custom error handler function to a string variable that I can use.

                      rayge99;10988954 wrote:

                      NOW the question is how to output the data from the custom error handler function to a string variable that I can use.

                      Take a look at the manual page for the $php_errormsg variable here: [man]reserved.variables.phperrormsg[/man]. Note that even if that directive isn't enabled, you could still replicate its functionality manually by doing something like:

                      $GLOBALS['php_errormsg'] = 'some error message';

                      inside your error handler.

                        DUH Great idea on using GLOBALS! 🙂 that will do the trick!

                          bradgrafelman;10988952 wrote:

                          no accessor-type function that I can use to check if the next action will cause an error, and yet there is no way to (gracefully/quietly) 'catch' any errors that due actually occur when you actually perform that action

                          But XMLReader is based on libXML, so doesn't [man]libxml_use_internal_errors[/man] and [man]libxml_get_last_error[/man] work?

                            johanafm;10989052 wrote:

                            But XMLReader is based on libXML, so doesn't [man]libxml_use_internal_errors[/man] and [man]libxml_get_last_error[/man] work?

                            Good call - never realized those libxml functions were available.

                              Write a Reply...