For me, this is OK as long as this does not cause any PHP Fatal error.
Rather than to test the object, I have preferred to test if the method exists.
You can achieve the same thing by testing if the getCode() function exists like what I did with the get() function.

if ( method_exists( $error, 'get')) {
     $code = $error->get('code'); 
}
else ( method_exists( $error, 'getCode')) {
    $code = $error->getCode();
}
else {
   $code = '';
}