Skip to content

Instantly share code, notes, and snippets.

@Reinmar Reinmar/catch_me.php
Created Oct 19, 2011

Embed
What would you like to do?
PHP sucks
<?php
namespace x;
require 'catch_me2.php';
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
try {
\y\shittyCode();
}
catch (Exception $e) {
echo 'Gotcha!';
}
//Guess what? Uncaught exception 'Exception' with message 'catch me if you can' :)
//No error caused by lack of Exception class in this namespace
<?php
namespace y;
function shittyCode() {
throw new \Exception('catch me if you can');
}
@singles

This comment has been minimized.

Copy link

commented Oct 19, 2011

You are throwing \Exception which is base exception class from PHP standard library. You're catching Exception from namespace x. Replace Exception with \Exception in catch_me.php at line 11 and everything works as it should.

And please change snippet name to "Reinmar can't read manual." :P

@Reinmar

This comment has been minimized.

Copy link
Owner Author

commented Oct 19, 2011

I know what's the difference between \Exception and Exception, and already fixed that in my script. But the problem is with lack of information - there's silent failure, although in namespace x there's no Exception class.

@singles

This comment has been minimized.

Copy link

commented Oct 19, 2011

You can show that without second file - that was suggesting something different. Just throw new \Exception and works the same. But you're right, there's no info about not defined exception class, and I agree it should be one. It's mentioned in manual.

@Reinmar

This comment has been minimized.

Copy link
Owner Author

commented Oct 19, 2011

I know I can, but then it wouldn't be so tricky :> In the case I was working on the exception was thrown far far away, so it took me a while before I've found wtf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.