Skip to content

Instantly share code, notes, and snippets.

@Reinmar
Created October 19, 2011 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Reinmar/1298274 to your computer and use it in GitHub Desktop.
Save Reinmar/1298274 to your computer and use it in GitHub Desktop.
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
Copy link

singles 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
Copy link
Author

Reinmar 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
Copy link

singles 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
Copy link
Author

Reinmar 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