php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39706 __destruct doesn't get called on uncaught Exceptions
Submitted: 2006-12-01 16:34 UTC Modified: 2006-12-04 20:30 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:0 (0.0%)
From: magic dot bitbucket at gmail dot com Assigned: dmitry (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.0 OS: GNU/Linux - Slackware 11
Private report: No CVE-ID: None
 [2006-12-01 16:34 UTC] magic dot bitbucket at gmail dot com
Description:
------------
When throwing an Exception and not catching it, Object destructors don't get called.
I don't know if this is a bug or a feature, but surely it isn't expected behaviour and it isn't documented.
This allows Objects to go out of scope without the destructor being called and makes destructors useless as cleanup has to be done manually before even thinking about throwing an Exception.

Reproduce code:
---------------
#!/usr/bin/php
<?php
class ExceptionTest {
	public function __construct(){}
	public function __destruct() { echo "hilfe mein leben!\n"; }
}
$test=new ExceptionTest();
throw new Exception('why are the destructors not called?', 666);
?>

Expected result:
----------------
I would expect __destruct() to be called when throwing the Exception.

Actual result:
--------------
__destruct() isn't called.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-04 13:47 UTC] dmitry@php.net
PHP doesn't call destructors after fatal errors. The "Uncaught exception" is an ordinary fatal error too, so the behavior is expected.
 [2006-12-04 20:14 UTC] magic dot bitbucket at gmail dot com
Sorry for asking, probably this isn't the right place for 
this discussion, but why is an uncaught exception a fatal 
error?
Fatal errors usually only occur when running out of 
memory, calling undefined methods, syntax errors ... ?
An uncaught exception IMHO doesn't really fit into this 
category.
Since there is no way to enforce try blocks in PHP when 
writing a library that someone else uses, 
you can't guarantee the exception will be caught and thus 
destructors are quite useless for resource cleanup (temp 
files, transactions).
Sorry for reopening but I really don't understand the 
rationale. And sorry for my english ^^
 [2006-12-04 20:30 UTC] tony2001@php.net
Well, because uncaught exception must halt the execution right away. What would be the sense in exceptions if uncaught exception would mean notice or warning?
It also has to halt the script because further execution can generate another exceptions and so on.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 14 21:01:33 2024 UTC