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!!