Thomas Banderas
Thomas Banderas

Reputation: 1739

Symfony2 dont show 500 error while throwing exception

Look at this simple code:

  public function test(){
    if(sth){ //true
      throw new Exception("Bla Bla");
    }
  }

And function in controller:

  try {
    test();
  } catch (Exception $e) {
    $toSend = $e->getMessage();
    return $this->render('view.html.twig', array('toSend' => $toSend);
  }

I want that function in controller render view.twig.html and send 'Bla Bla' in toSend variable.

While I'm getting 500 Internal Server Error - Exception

Best Regards

Upvotes: 0

Views: 261

Answers (1)

Hendra Huang
Hendra Huang

Reputation: 528

I see there is some syntax error in your code.

  1. You have to write throw new \Exception("Bla Bla") instead of throw Exception("Bla Bla")
  2. You have to write catch (\Exception $e) instead of catch (Exception $e)

If you dont put "\" before Exception class, it means that it will find Exception class in your current namespace. If it can't find it, it will throw exception.

Upvotes: 4

Related Questions