Infinite throwing in constructor
What's going on with a php script that looks like this?
class FooException extends Exception
{
public function __construct() {
throw new FooException;
}
}
He is probably the same as
while (TRUE) {
new Exception();
}
Are they just timeouts when execution time is exceeded or fails with some fatal error?
+2
a source to share
3 answers
In the first case, nothing happens because you never throw an exception.
In the second case, no exception is thrown, so you just end up with a normal infinite loop.
However, if you change the first example by adding this line to the end:
throw new FooException();
It calls an endless loop that will eventually consume all memory:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 44 bytes)
+2
a source to share
You can just test it, but I think it throws a fatal error when you throw an exception and the exception has already been thrown.
EDIT: Ok, I was confused. Here's where you get a fatal out of memory error:
class FooException extends Exception
{
public function __construct() {
throw new FooException;
}
}
throw new FooException();
What I described happens when you throw an exception in your exception handler.
0
a source to share