I just wanted to double-check if what I just found is default PHP behaviour, because I cannot explain it otherwise. Say you build a custom PHP Exceptions class:
<?php
namespace XYZ;
if ( ! class_exists( 'XYZ\CustomException' ) ) {
class CustomException extends \Exception {
public function __construct( int $code ) {
parent::__construct( "The error code is: $code", $code );
}
}
}
?>
You then launch a PHP script with a content like:
try {
throw new CustomException(1);
} catch ( \Exception $e ) {
echo "regular exception thrown";
} catch ( CustomException $e ) {
echo "custom exception thrown";
}
When running that block, I get "regular exception thrown".
When I reverse the catch blocks of the script to:
try {
throw new CustomException(1);
} catch ( CustomException $e ) {
echo "custom exception thrown";
} catch ( \Exception $e ) {
echo "regular exception thrown";
}
I get "custom exception thrown".
Is it actually default in PHP that child classes of the Exception class will be caught by the parent Exception handler, if the parent Exception class catch block is written first?? Or is this some sort of weird config that is unusual?