title: ZF2 how to throw an exception in an attached event listener
body: <p>I am trying to throw (and catch elsewhere but I don't get that far) an exception from within a closure event listener set in the Module.php::onBootstrap method.</p>
  
  <p>What I get is a time out and dump of the eventmanager with the last bits of the trace showing the em trying to run the closure. The listener runs if the exception is not thrown. </p>
  
  <p>Any tips or clues would be very much appreciated.</p>
  
  <pre><code>public function onBootstrap(\Zend\Mvc\MvcEvent $e)
  {
  $sharedManager = $e-&gt;getApplication()
      -&gt;getEventManager()
      -&gt;getSharedManager();
  $sm = $e-&gt;getApplication()-&gt;getServiceManager();
  $em = $sm-&gt;get('doctrine.entitymanager.orm_default');
  
  $sharedManager-&gt;attach(__NAMESPACE__, 'masterform-submit', function ($e) use($em)
      {
           ... 
          throw new \Exception('Did this work');
           ...
      }
      );
  }
  </code></pre>
  
  <p>And from a controller:</p>
  
  <pre><code>try{
      $this-&gt;getEventManager()-&gt;trigger('masterform-submit', $this, $data);
  } catch ( Exception $e ) {
      error_log($e-&gt;getMessage());
  }
  </code></pre>