{"id":254,"date":"2010-03-31T18:03:14","date_gmt":"2010-03-31T10:03:14","guid":{"rendered":"http:\/\/www.genepeng.com\/index.php\/254"},"modified":"2010-03-31T18:14:05","modified_gmt":"2010-03-31T10:14:05","slug":"do-not-throw-exception-in-exception_handler-function-and","status":"publish","type":"post","link":"https:\/\/www.genepeng.com\/index.php\/254","title":{"rendered":"Do not throw exception in exception_handler function and __destruct without try-catch"},"content":{"rendered":"<pre><font color=\"#0000ff\">Do NOT throw exception in exception_handler function<\/font>, if you do it like below, <\/pre>\n<pre>the error \u201c<b>Fatal error<\/b>: Exception thrown without a stack frame in <b>Unknown<\/b> on line <\/pre>\n<pre><b>0<\/b>\u201d will occur<\/pre>\n<pre><pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">set_exception_handler('handle_exception');\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">function handle_exception($e)\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">{\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">    throw new Exception('throwed in handle_exception');\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">}\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">throw new Exception('error occur');<\/pre>\n<p>if you try-catch the Exception in exception_handler function, it will be ok, the below code will work well<\/p>\n<pre><pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">set_exception_handler('handle_exception');\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">function handle_exception($e)\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">{\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">    try {\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">        throw new Exception('throwed in handle_exception');\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">    }\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">    catch (Exception $e) {\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">        die('catch exception in handle_exception function');\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">    }\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">}\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">throw new Exception('error occur');<\/pre>\n<pre><font color=\"#0000ff\">Do NOT throw Exception in __destruct like below, or \u201c<b>Fatal error<\/b>: Exception thrown<\/font><\/pre>\n<pre><font color=\"#0000ff\">without a stack frame in <b>Unknown<\/b> on line <b>0<\/b>\u201d will occur.<\/font><\/pre>\n<pre><pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">class Test {\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">    function __construct () {}\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">    function __destruct() {\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">        throw new Exception('throwed in __destruct');\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">    }\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">}\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">$test = new Test();<\/pre>\n<p>but it will work well when try-catch it like below<\/p>\n<pre><pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">class Test {\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">    function __construct () {}\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">    function __destruct() {\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">        try {\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">            throw new Exception('throwed in __destruct');\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">        }\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">        catch (Exception $e) {\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">            die('catch exception in __destruct');\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">        }\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">        \n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">    }\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">}\n<\/pre>\n<pre style=\"font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">$test = new Test();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Do NOT throw exception in exception_handler function, if you do it like below, the error \u201cFatal error: Exception thrown without a stack frame in Unknown on line 0\u201d will occur set_exception_handler(&#8216;handle_exception&#8217;); function handle_exception($e) { throw new Exception(&#8216;throwed in handle_exception&#8217;); } throw new Exception(&#8216;error occur&#8217;); if you try-catch the Exception in exception_handler function, it will be &#8230; <a title=\"Do not throw exception in exception_handler function and __destruct without try-catch\" class=\"read-more\" href=\"https:\/\/www.genepeng.com\/index.php\/254\" aria-label=\"More on Do not throw exception in exception_handler function and __destruct without try-catch\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/254"}],"collection":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":3,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":257,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions\/257"}],"wp:attachment":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}