Handle exception and continue executing

孤街醉人 提交于 2021-02-07 09:28:30

问题


[EDIT - Disclaimer: This is a really bad idea, see the accepted answer for an actual solution.]

I define my own exception handler using set_exception_handler() function. After the handler execution, I need the script to continue. Is there any way to do it?

Disclaimer: I know try-catch blocks but I need to process Exceptions dynamicaly. Every calling of Clazz::foo() specifies its own exceptions which should be caught by my handler. That's the reason I can't use it.

Example:

class Clazz {

    private static $exceptions;

    public static function foo(array $exceptions) {
        set_exception_handler(array(__CLASS__, "exception_handler"));
        self::$exceptions = $exceptions;
        throw new RandomException;
        echo "I need this to be printed!";
    }

    public static function exception_handler($exception) {
        // process the exception in my way...
        // if $exception in self::$exceptions than 1, else 2, fi
        restore_exception_handler();
        // continue in some way, like it has never happenned
    }

}

回答1:


This is just bad idea. I just hope you don't understand how Exceptions work and you're not meaning the question.

First of all, setting exception handler... Exception handler is called when the exceptions is propagated to main script (actually out of it) and your script is therefore done:

Sets the default exception handler if an exception is not caught within a try/catch block. Execution will stop after the exception_handler is called.

You should either use what's Xeoncross suggesting, but I think you have problem with called function/method that is throwing exceptions so you can do this:

class Clazz {

    private static $exceptions;

    public static function foo(array $exceptions) {
        set_exception_handler(array(__CLASS__, "exception_handler"));
        self::$exceptions = $exceptions;
        try {
            throw new RandomException;
        } catch( Exception $e){
            self::exception_handler( $exception);
        }
        echo "I need this to be printed!";
    }

    public static function exception_handler(Exception $exception) {
    }
}



回答2:


Just do something like this in you startup file

/**
 * Set the default exception handler.
 *
 * @param Exception $exception The exception to handle
 */
$handler = function(Exception $exception)
{
    //print $exception;
};

set_exception_handler($handler);

/**
 * Register the PHP error handler. All PHP errors will fall into this
 * handler, which will convert the error into an ErrorException object
 * and pass the exception into the common exception handler.
 *
 * After all, there should never be any errors in our application. If
 * there are then we need to know about them and fix them - not ignore
 * them.
 *
 * Notice, this function will ignore the error if it is less than the
 * current error reporting level.
 */
set_error_handler(function($code, $error, $file, $line) use ($handler)
{
    if((error_reporting() & $code) === 0) return TRUE;

    $handler(new ErrorException($error, $code, 0, $file, $line));
});

/**
 * Register the PHP shutdown handler. This function will be called
 * at the end of the PHP script or on a fatal PHP error. If an error
 * has occured, we will convert it to an ErrorException and pass it
 * to the common exception handler for the framework.
 */
register_shutdown_function(function() use ($handler)
{
    if($error = error_get_last())
    {
        extract($error, EXTR_SKIP);
        $handler(new ErrorException($message, $type, 0, $file, $line));
    }
});


/**
 * Setting the PHP error reporting level to -1 essentially forces PHP to
 * report every error, and is guranteed to show every error on future
 * versions of PHP. This will insure that our handlers above are
 * notified about everything.
 */
error_reporting(-1);

/**
 * At the same time we want to disable PHP's default error display since
 * we are now using our own.
 */
ini_set('display_errors', 'Off');



回答3:


Nope. Fortunately there is no way to do that




回答4:


Generally, this is a bad idea because code throwing an exception is expecting that any following code is not executed, and I guess that's the reason why it is not implemented in php.

But maybe that may help doing what you are trying to do:

function try_throw($exception) {
    if (!Clazz::exception_handler($exception))
        throw($exception);

}

class Clazz {

    private static $exceptions;

    public static function foo(array $exceptions) {
        set_exception_handler(array(__CLASS__, "exception_handler"));
        self::$exceptions = $exceptions;
        try_throw(new RandomException);
        echo "I need this to be printed!";
    }

    public static function exception_handler($exception) {
        // process the exception in my way...
        // if $exception in self::$exceptions than 1, else 2, fi
        return true;
        // continue in some way, like it has never happenned
        // to instead throw the exception the normal way
        return false;
    }

}


来源:https://stackoverflow.com/questions/9574583/handle-exception-and-continue-executing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!