Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4612118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:21:38+00:00 2026-05-22T01:21:38+00:00

Please consider the following code: <?php class MyException extends Exception {} function global_exception_handler($exception) {

  • 0

Please consider the following code:

<?php

class MyException extends Exception {}

function global_exception_handler($exception)
{
    switch (get_class($exception)) {
    case 'MyException':
        print "I am being handled in a unified way.\n";
        break;
    default:
        $backtrace = debug_backtrace();
        $exception_trace_object = $backtrace[0]['args'][0];
        var_dump($exception_trace_object);
        print "----\n";
        $reflected_exception_trace_object = new ReflectionObject($exception_trace_object);
        $reflected_trace_property = $reflected_exception_trace_object->getProperty('trace');
        $reflected_trace_property->setAccessible(true);
        var_dump($reflected_trace_property);
        print "----\n";

        // NOT WORKING, I STUCK HERE.
        var_dump($reflected_trace_property->getValue($reflected_trace_property));

        throw $exception;
    }
}

set_exception_handler('global_exception_handler');

function function1()
{
    function2();
}

function function2()
{
    function3();
}

function function3()
{
    throw new Exception();
}

function1();

?>

What I’m trying to do is handling various types of exceptions in a unified way across various files by simply setting up a global exception handler without having to write any boilerplate code (except the header and footer includes which are present in every file).

The problem is that when the thrown exception type is not handled by the global exception handler and I want to rethrow the exception, the stack trace gets lost which is a limitation of using set_exception_handler().

I can retrieve the stack trace by using debug_backtrace() but I cannot access its relevant private members to be able to print it appropriately.

This is what the above script produces:

object(Exception)#1 (7) {
  ["message":protected]=>
  string(0) ""
  ["string":"Exception":private]=>
  string(0) ""
  ["code":protected]=>
  int(0)
  ["file":protected]=>
  string(28) "/home/laci/download/test.php"
  ["line":protected]=>
  int(42)
  ["trace":"Exception":private]=>
  array(3) {
    [0]=>
    array(4) {
      ["file"]=>
      string(28) "/home/laci/download/test.php"
      ["line"]=>
      int(37)
      ["function"]=>
      string(9) "function3"
      ["args"]=>
      array(0) {
      }
    }
    [1]=>
    array(4) {
      ["file"]=>
      string(28) "/home/laci/download/test.php"
      ["line"]=>
      int(32)
      ["function"]=>
      string(9) "function2"
      ["args"]=>
      array(0) {
      }
    }
    [2]=>
    array(4) {
      ["file"]=>
      string(28) "/home/laci/download/test.php"
      ["line"]=>
      int(45)
      ["function"]=>
      string(9) "function1"
      ["args"]=>
      array(0) {
      }
    }
  }
  ["previous":"Exception":private]=>
  NULL
}
----
object(ReflectionProperty)#3 (2) {
  ["name"]=>
  string(5) "trace"
  ["class"]=>
  string(9) "Exception"
}
----
NULL

Fatal error: Exception thrown without a stack frame in Unknown on line 0

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:21:39+00:00Added an answer on May 22, 2026 at 1:21 am

    Check the comments on http://php.net/manual/de/function.set-exception-handler.php there are several similar issues and solutions there. For your code I see two possible solutions:

    Change your code into

    function global_exception_handler($exception = NULL)
    

    or add a description to

    throw new Exception('Testing here...');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please pardon me for a n00bish question. Please consider the following code: public class
Please consider the following code: type TFoo1 = class public procedure DoSomething1; end; TFoo2
Please consider the following code: class Student { } enum StudentType { } static
Please clarify my doubt in Hashset. Consider the following code, class Person { String
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
Consider the following code <?php $username = root; $password = ; $host = localhost;
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.