public static function handleException(Exception $e, $encoding)
    {
        // PHPTAL's handler is only useful on fresh HTTP response
        if (PHP_SAPI !== 'cli' && !headers_sent()) {
            $old_exception_handler = set_exception_handler(array(new PHPTAL_ExceptionHandler($encoding), '_defaultExceptionHandler'));

            if ($old_exception_handler !== NULL) {
                restore_exception_handler(); // if there's user's exception handler, let it work
            }
        }
        throw $e; // throws instead of outputting immediately to support user's try/catch
    }