jsf - Handling service layer exception in Java EE frontend method -


i maintain web application have page jsf tag <f:event. have rewrote method in service class throw business exception. however, when business exception thrown, isn't caught in managed bean , exception showed on page. seems code try/catch doesn't work.

in xhtml:

<f:event listener="#{resourcebean.init(enrollment)}" type="prerenderview" /> 

listener method in managed bean:

private boolean cancreateresource;  public void init(enrollment enrollment) {     (...)      try {         cancreateresource = resourceservice.cancreateresource(enrollment);     } catch (businessexception e) {         cancreateresource = false;     } } 

method in service class:

public boolean cancreateresource(enrollment enrollment) {     if (...) {         if (mandateservice.iscoordinator(user, course)) {             return true;         } else {             throw new businessexception("undefined business rule.");         }     }      return false; } 

from read on other sites, suppose have implement jsf's handler class. , how?


edited

obs 1: businessexception class extends runtimeexception class.

obs 2: attribute cancreateresource created control render of button.

it's because threw runtimeexception ejb.

when such runtimeexception not annotated @applicationexception, ejb container wrap in javax.ejb.ejbexception , rethrow it. done because runtime exceptions usually used indicate bugs in code logic, i.e. programmer's mistakes , not enduser's mistakes. know, nullpointerexception, illegalargumentexception, indexoutofboundsexception, numberformatexception , friends. allows ejb client have 1 catch-all point such runtime exceptions, catch (ejbexception e) { there's bug in service layer or in way how using it! }

if had tried catch (exception e) , inspected actual exception, you'd have noticed that.

fix businessexception class accordingly add annotation, recognized real application exception , not wrapped in ejbexception:

@applicationexception(rollback=true) public class businessexception extends runtimeexception {     // ... } 

do note in case throw non-runtimeexception, still need keep annotation on that, explicitly rollback=true, because default wouldn't perform rollback, on contrary runtimeexception without annotation.

@applicationexception(rollback=true) public class businessexception extends exception {     // ... } 

summarized:

  1. runtimeexception thrown transactional ejb method perform full rollback, exception wrapped in ejbexception.
  2. runtimeexception @applicationexception transactional ejb method perform full rollback when rollback=true explicitly set.
  3. exception transactional ejb method not perform full rollback.
  4. exception @applicationexception transactional ejb method perform full rollback when rollback=true explicitly set.

note @applicationexception inherited on subclasses of custom exception, don't need repeat on of them. best have abstract class. see examples in related question linked below.

see also:


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -