How does the use of namespaces affect the way exceptions are caught in PHP?
When using namespaces in PHP, it's important to remember that exceptions thrown within a namespace must be caught within the same namespace. If an exception is thrown in one namespace and caught in another, PHP will not be able to find the exception class and will result in a fatal error. To solve this issue, make sure to catch exceptions within the same namespace where they are thrown.
namespace MyNamespace;
class CustomException extends \Exception {}
try {
throw new CustomException('An error occurred');
} catch (CustomException $e) {
echo 'Caught exception: ' . $e->getMessage();
}