View Single Post
Old Dec 27th, 2006, 1:00 AM   #1
headzoo
Newbie
 
Join Date: Oct 2006
Posts: 16
Rep Power: 0 headzoo is on a distinguished road
Catching exceptions up the stack

Howdy all,
The problem I'm having is catching an exception at the top layer of a script, that is thrown from a lower level. Exceptions *should* travel all the way up the stack, looking for a catch statement, until the end of the stack is reached. But that's not working for me.

Here is a code example:
class Foo
{
	public function __construct()
	{
		throw new Exception('Oh no!');
	}
}

class Bar
{
	public function __construct()
	{
		$Foo = new Foo;
	}
}

class MyClass
{
	public function __construct()
	{
		$Bar = new Bar;
	}
}

try {
	$MC = new MyClass;
} catch (Exception $e) {
	die($e->getMessage());
}

Even though the exception is thrown in the Foo class, which is the third class initiated, the exception *should* bubble-up and get caught in the try...catch statement. But it doesn't. PHP will die with a uncaught exception fatal error.

Actually the above code works just fine. But the code I'm working on now is basically the same, only it's a lot more code. It's an object created by an object, created by an object, created by an object, which finally has a try...catch statement. The deepest object throws the exception, and it's not bubbling-up to the try...catch statement.

So is there a limit on how far up the stack PHP will go to find a try...catch clause?
headzoo is offline   Reply With Quote