Php Symfony 2.5-我无法捕获异常
我正在基于symfony 2.5构建一个新项目的最后阶段,我认为拥有自定义错误页面会很好。我创建了自己的异常侦听器,请参见下面的代码: 这是服务声明:Php Symfony 2.5-我无法捕获异常,php,symfony,pdo,exception-handling,Php,Symfony,Pdo,Exception Handling,我正在基于symfony 2.5构建一个新项目的最后阶段,我认为拥有自定义错误页面会很好。我创建了自己的异常侦听器,请参见下面的代码: 这是服务声明: kernel.listener.core_exception_listener: class: MyCompany\CoreBundle\Listener\ExceptionListener arguments: [@templating, @kernel] tags: - { name: kernel.e
kernel.listener.core_exception_listener:
class: MyCompany\CoreBundle\Listener\ExceptionListener
arguments: [@templating, @kernel]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
<?php
namespace MyCompany\CoreBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ExceptionListener
{
protected $templating;
protected $kernel;
public function __construct(EngineInterface $templating, $kernel)
{
$this->templating = $templating;
$this->kernel = $kernel;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
// provide the better way to display a enhanced error page only in prod environment, if you want
if ('prod' == $this->kernel->getEnvironment()) {
// exception object
$exception = $event->getException();
// new Response object
$response = new Response();
if($exception instanceof NotFoundHttpException) {
$template = 'CoreBundle:Exception:404.html.php';
} else {
$template = 'CoreBundle:Exception:exception.html.php';
}
$response->setContent(
// create you custom template AcmeFooBundle:Exception:exception.html.twig
$this->templating->render(
$template,
array('exception' => $exception)
)
);
// HttpExceptionInterface is a special type of exception
// that holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(500);
}
// set the new $response object to the $event
$event->setResponse($response);
}
}
}
下面是实际的PHP类:
kernel.listener.core_exception_listener:
class: MyCompany\CoreBundle\Listener\ExceptionListener
arguments: [@templating, @kernel]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
<?php
namespace MyCompany\CoreBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ExceptionListener
{
protected $templating;
protected $kernel;
public function __construct(EngineInterface $templating, $kernel)
{
$this->templating = $templating;
$this->kernel = $kernel;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
// provide the better way to display a enhanced error page only in prod environment, if you want
if ('prod' == $this->kernel->getEnvironment()) {
// exception object
$exception = $event->getException();
// new Response object
$response = new Response();
if($exception instanceof NotFoundHttpException) {
$template = 'CoreBundle:Exception:404.html.php';
} else {
$template = 'CoreBundle:Exception:exception.html.php';
}
$response->setContent(
// create you custom template AcmeFooBundle:Exception:exception.html.twig
$this->templating->render(
$template,
array('exception' => $exception)
)
);
// HttpExceptionInterface is a special type of exception
// that holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(500);
}
// set the new $response object to the $event
$event->setResponse($response);
}
}
}
要捕获pdo异常
,必须对其命名。
将其添加为use语句或在调用时添加前导\。
比如,
try {
// your code
} catch (\PDOException $pdo_ex) {
die("Oops, ".$pdo_ex->getMessage());
}
这是帮助我处理预期错误的代码
}运行时,条令会引发异常-我的测试包括mysql服务器不可用的场景。我无法插入try-catch代码块,因为这意味着我必须更改代码核心。