Home >Backend Development >PHP Tutorial >PHP error: Exception thrown without a stack frame in Unknown on line 0[Reprinted],thrownstack_PHP tutorial

PHP error: Exception thrown without a stack frame in Unknown on line 0[Reprinted],thrownstack_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:141326browse

PHP error: Exception thrown without a stack frame in Unknown on line 0 [Reprinted], thrownstack

From: NetEase Blog

As far as I know, In two cases, PHP will report the error Exception thrown without a stack frame in Unknown on line 0:

1) Exception capture uses the set_exception_handler guide, and another Exception is executed inside the Exception

As shown in the following code , this problem will occur:

http://de.php.net/manual/de/function.set-exception-handler.php#88082

<span>function</span> error_handler(<span>$code</span>, <span>$message</span>, <span>$file</span>, <span>$line</span><span>)
{
    </span><span>if</span> (0 == <span>error_reporting</span><span>())
        </span><span>return</span><span>;
        
    </span><span>throw</span> <span>new</span> ErrorException(<span>$message</span>, 0, <span>$code</span>, <span>$file</span>, <span>$line</span><span>);
}
</span><span>function</span> exception_handler(<span>$e</span><span>)
{
    </span><span>//</span><span> ... normal exception stuff goes here</span>
    <span>print</span> <span>$undefined</span>; <span>//</span><span> This is the underlying problem</span>
<span>}
</span><span>set_error_handler</span>("error_handler"<span>);
</span><span>set_exception_handler</span>("exception_handler"<span>);
</span><span>throw</span> <span>new</span> <span>Exception</span>("Just invoking the exception handler");

Print $undefined in the exception_handler function; This line itself will throw an exception, and he calls the exception_handler function of set_exception_handler, creating an infinite loop.

Solution: Do not execute another Exception inside an Exception

The above problem can be solved using try...catch. For example, the exception_handler can be changed to the following:

<span>function</span> exception_handler(<span>$e</span><span>)
{
    </span><span>try</span><span>
    {
        </span><span>//</span><span> ... normal exception stuff goes here</span>
        <span>print</span> <span>$undefined</span>; <span>//</span><span> This is the underlying problem</span>
<span>    }
    </span><span>catch</span> (<span>Exception</span> <span>$e</span><span>)
    {
        </span><span>print</span> <span>get_class</span>(<span>$e</span>)." thrown within the exception handler. Message: ".<span>$e</span>->getMessage()." on line ".<span>$e</span>-><span>getLine();
    }
}</span>

2) Throw an exception in the destructor

Refer to this bug: http://bugs.php.net/bug.php?id=33598

The following code will report this error:

<span>class</span><span> test {
    </span><span>function</span><span> __construct() {
        </span><span>echo</span> "Construct\n"<span>;
    }

    </span><span>function</span><span> greet() {
        </span><span>echo</span> "Hello World\n"<span>;
    } 

    </span><span>function</span><span> __destruct() {
        </span><span>echo</span> "Destruct\n"<span>;
        </span><span>throw</span> <span>new</span> <span>Exception</span>( 'test'<span> );
    }
}

</span><span>$test</span> = <span>new</span><span> test();
</span><span>$test</span>->greet();

Current solution:

1. Do not throw exceptions in the destructor.

2. Since the destructor is executed when exiting, you must manually unset this class and catch the exception.

For example, in the above example, add a line of unset($test) at the end, then the program will report throw new Exception( 'test' ); There is an error in this line, and then catch this exception.

The above two situations will occur in PHP 5.2.11 version. As for the reason, I think PHP may handle it this way. PHP bug 33598 was reported in 2005. The bug Status is Closed, indicating that the official does not think this is the case. A bug, or a bug not handled properly.

What’s wrong with me opening the session?

If this file is not included in another file. . You can try the following.

Do not encode the file in UTF-8. .

Move your code below to the top. . There is no HTML code in the PHP file you included anyway.

Check your php.ini and pay attention to the path where the SESSION temporary file is saved, whether it can be accessed by PHP.

Why does thinkphp appear Chinese garbled characters when reading sqlite data? Guidance

The encodings are different, some are UTF-8, and some are Unicode, so they will appear as garbled characters. When inserting into the database, set it to UTF-8. Just use UTF-8 when reading it out.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/850752.htmlTechArticlePHP error: Exception thrown without a stack frame in Unknown on line 0[Reprinted],thrownstack From: NetEase Blog My current understanding is that in two cases, PHP will report Exceptio...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn