public function dispatch()
    {
        $response = null;
        try
        {
            $this->resolveApp();
            $this->getTemplate();
            $this->request->startSession($this->vhost->getHost(), $this->config);
            FlashMessage::setStorage($this->request->session);

            $this->setupLocale();

            if ($this->route === null)
                throw new HTTPError(404, 'Could not resolve ' . $this->url);

            $app = new AppRunner($this->app, $this->arguments);
            $app->setVariables($this->variables);
            $app->setVariable('dispatcher', $this);

            $app->execute();
        }
        catch (Throwable $e)
        {
            if (!($e instanceof Response))
                $e = new HTTPError(500, "Exception of type " . get_class($e) . " thrown: " . $e->getMessage(), null, $e);

            if ($e instanceof HTTPError)
                $this->prepareErrorResponse($e);

            $response = $e;
        }
        return $response;
    }