<?php
try {
$attrs = $xml->attributes();
$code = $attrs['_Code'];
}
catch (Exception $e)
{
$code = '';
}
?>
Gets me:
Fatal error: Call to a member function attributes() on a non-object on
line 6
Why am I getting errors thrown on code wrapped in a try-catch??
NOTE: It is possible to avoid this error by using the following code. (The question is not about avoiding the error, but why it’s not being caught- still I thought I’d share the non-erroring code anyway)
if (is_object($xml) && method_exists($xml,'attributes')) {
$attrs = $xml->attributes();
$code = !empty($attrs['_Code'])?$attrs['_Code']:'';
}
else {
$code = '';
}
PHP fatal errors cannot be caught. I don’t know the specifics of what you’re doing, but you’ll have to figure out some other way to test whether
$xml->attributes()will work or not.Also, swallowing every error and not logging it anywhere is bad practice because when stuff starts breaking you won’t have any idea of why.