tags:

views:

35

answers:

3

What happens to a php script which goes like this?

class FooException extends Exception
{

    public function __construct() {
        throw new FooException;
    }
}

It's probably same as

while (TRUE) {
    new Exception();
}

It simply time outs when execution time is exceeded, or fails with some fatal error?

A: 

You could just test it, but I think it throws a fatal error when you throw an exception and an exception has already been thrown.

EDIT: OK, I was confused. You get an out-of-memory fatal error here:

class FooException extends Exception
{

    public function __construct() {
        throw new FooException;
    }
}

throw new FooException();

What I described happens when you throw an exception in an exception handler.

Artefacto
+2  A: 

In the first case nothing happens, because you never construct the exception.

In the second case the exception is not thrown so you just get an ordinary infinite loop.

However if you modify the first example by adding this line at the end:

throw new FooException();

It causes an infinite loop which eventually consumes all the memory:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 44 bytes)
Mark Byers
Of course the exception has to be thrown before, thats obvious. The while loop was there just as for a demonstration, however I should have pointed it out. Thanks anyway, just wanted to know if php checks it and fails with fatal error.
Mikulas Dite
Interesting that it leads to the memory error and not the maximum recursion depth ....
Techpriester
+1  A: 

I tried it on PHP 5.2.8 (adding a new FooException(); at the end) and ran out of memory:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40 bytes) on line 5

Michael Mrozek