php中exit()触发的析构函数引发异常时,如何打印消息?

php中exit()触发的析构函数引发异常时,如何打印消息?,php,exception-handling,Php,Exception Handling,我有如下PHP代码: <?php class MyDestructableClass { function __destruct() { print "Destroying MyDestructableClass"; throw new Exception('Intentionally thrown exception, can it be caught?'); } } $obj = new MyDestructableClass(); exit

我有如下PHP代码:

<?php
class MyDestructableClass {
   function __destruct() {
       print "Destroying MyDestructableClass";
       throw new Exception('Intentionally thrown exception, can it be caught?');
   }
}

$obj = new MyDestructableClass();

exit;  // Triggers destructor eventually

?>
我想在析构函数发生时打印一条特殊消息,在退出时抛出一个异常。我不能修改MyDestructableClass本身的内容,我只想注意它的析构函数何时抛出异常

我尝试了一个异常处理程序:

<?php
class MyDestructableClass {
   function __destruct() {
       print "Destroying MyDestructableClass";
       throw new Exception('Intentionally thrown exception, can it be caught?');
   }
}

$obj = new MyDestructableClass();

function myExceptionHandler($exception)
{
  print "I noticed an exception was thrown, success!";
}

set_exception_handler('myExceptionHandler');

exit;  // Triggers destructor eventually

?>
但没有任何指纹

我还尝试了关机功能:

<?php
class MyDestructableClass {
   function __destruct() {
       print "Destroying MyDestructableClass";
       throw new Exception('Intentionally thrown exception, can it be caught?');
   }
}

$obj = new MyDestructableClass();

function myShutdownFunction()
{
  if (error_get_last() != NULL)  // Only want to react to errors, not normal shutdown
  {
    print "I noticed an exception was thrown, success!";
  }
}

register_shutdown_function('myShutdownFunction');

exit;  // Triggers destructor eventually

?>
但没有任何指纹


什么技术可以注意到由exit启动的析构函数中的异常?

这对我很有用。但我不知道这是否是你想要的

<?php

class MyDestructableClass {
    function __destruct() {
        print "Destroying MyDestructableClass";
        throw new Exception('Intentionally thrown exception, can it be caught?');
    }
}


function RunEverything(){
    $obj = new MyDestructableClass();
}

try {
    RunEverything();

} catch (Exception $e){
    echo 'My error has been thrown';
}


exit;
/*
 */

?

我会澄清问题。你的代码正常,但我需要出口作为析构函数的触发器。我可以问一下为什么吗?它必须是出口吗?或者可能是我的出口;我还没有使用我的_出口的解决方案;但绝对知道可能是件好事。我这里的问题是我所面临的全部问题的一个简化版本,其中包含的库强制我退出。您知道哪些实例可能触发异常吗?你能在退出之前强制执行析构函数吗;?不幸的是,抛出异常的实例被多个外部类封装。