1

I am having an issue where an exception is being thrown but not caught in Module.php. The code that throws the exception is wrapped in a try/catch. There's nothing really fancy going on so I am assuming ZF2 has a quirk and/or reason for not allowing exceptions to be caught here.

The code is as follows (simplified as necessary):

class Module { public function onBootstrap(MvcEvent $e) { // Get service manager, etc. // Set up some environment stuff try { // exception is thrown } catch (\Exception $e) { echo $e->get_message(); exit; } } } 

Why is the exception not being caught?

In an ideal world there would be a way to catch this exception here. But if that is not possible, or is too convoluted to be worth the effort, an alternative process to fetching this data (regardless of source) as early in the page loading process as possible would be appreciated.

meta

I know the code in Module.php is supposed to be lightweight. But we have to fetch some data immediately before any action is performed as it will contain data vital to those actions to be performed. This data is cached after the first visit so every other page load will avoid this overhead.

I also have Googled this but apparently no one else has either experienced this, asked about it, or documented it.

2
  • My assumption would be that internally a different exception is triggered and caught someplace else and ZF2 creates a response object and returns that - ZF2 has internal error handling after all. Commented Nov 11, 2013 at 20:47
  • One alternative for fetching this data would be Zend's built in logger: framework.zend.com/manual/1.12/en/zend.log.overview.html Commented Nov 11, 2013 at 21:12

1 Answer 1

2

This code works just fine for me in a module class:

public function onBootstrap(MvcEvent $e) { try { // exception is thrown throw new \Exception('My exception here'); } catch (\Exception $e) { echo $e->getMessage(); exit; } } 

It displays the exception message and exits. One way to investigate what is happening is to use xdebug for step by step debugging. Just add a breakpoint in your Module and see what ZF is doing.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.