• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Application Exception in MDB

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

Can an MDB throw an application exception ?

Thanks
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any help on above post?
 
Ranch Hand
Posts: 133
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amirr Rafique wrote:Hello

Can an MDB throw an application exception ?

Thanks



The onMessage() method can throw only RuntimeExceptions therefore bean provider may throw an @ApplicationException which is the java.lang.RuntimeException.
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen couple of question about MDB throwing ApplicationException and as per my developed understanding MDB should not throw Application Exception as there is no client to receive this exception ....

Guys can any one please give any reference from specs
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EJB Core 14.3.4

MDB method throw Application Exception
and container re-throw Application Exception to resource adapter
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think whether you can throw an exception is not decided by the type of bean

the term "Application Exception" in EJB 3 means :

(1) "Checked Exception" (i.e subclass of Exception) may or may not defined using @ApplicationException

OR

(2) "Unchecked Exception" (i.e subclass of RuntimeException or Error) , only RuntimeException can be defined as an application exception using @ApplicationException (otherwise it is a system exception if not defined using @ApplicationException)

BUT cannot be subclass of java.rmi.RemoteException

================

whether you can throw an exception is decided by the basic rules of exception handling
Rules:

(1) Checked Exception
~ either declare it in the throws clause of your method or catch it in the body of your method

(2) RuntimeException which is defined as an application exception using @ApplicationException
~ either declare it in the throws clause of your method or catch it in the body of your method

(3) RuntimeException which is not defined as an application exception
~ is a normal RuntimeException which not need to be declare in throws clause or catch

(4) RemoteException
~ if your bean interface using @Remote without extends java.rmi.Remote interface, method define in the bean interface does not need to declare RemoteException in the throws clause. In this case, container throws EJBTransactionRollbackException or EJBException depends on the transaction context (refer to EJB Core spec 14.3)
~ if your bean interface extends java.rmi.Remote interface, method define in the bean interface MUST declare RemoteException in the throws clause. In this case container throws TransactionRollbackException or RemoteException depends on the transaction context (refer to EJB Core spec 14.3)

================

so what you should not confuse is the mess between
(a) bean method can throw which exception ?
(b) container can throw which exception ?

================

The onMessage() method can throw only RuntimeExceptions therefore bean provider may throw an @ApplicationException which is the java.lang.RuntimeException.



I can confirm that onMessage() can throw Unchecked Exception (RuntimeException and Error) , since this type of exception does not need to be declare in throws clause of onMessage() and does not need to be catch

From my studying, if a RuntimeExceptions that is defined as application exception using @ApplicationException, that exception need to be either declare in the throws clause of the bean method or catch in the bean method. As referring to the rules of exception handling that "you cannot throw any checked Exceptions other than those declared in the method you are overriding", so in your bean class which override the onMessage() method cannot declare additional exception in the throws clause, so if inside the onMessage() method do receive a ducking application exception from the method invoked from onMessage() method, it need to be catch. Am I right ?



 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

From my studying, if a RuntimeExceptions that is defined as application exception using @ApplicationException, that exception need to be either declare in the throws clause of the bean method or catch in the bean method. As referring to the rules of exception handling that "you cannot throw any checked Exceptions other than those declared in the method you are overriding", so in your bean class which override the onMessage() method cannot declare additional exception in the throws clause, so if inside the onMessage() method do receive a ducking application exception from the method invoked from onMessage() method, it need to be catch. Am I right ?



I think here you are referring to exceptions extending java.lang.Exception class and marked as @ApplicationException. As any exceptin extending RuntimeException does not need to be declare in throws clause

Please confirm
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think here you are referring to exceptions extending java.lang.Exception class and marked as @ApplicationException. As any exceptin extending RuntimeException does not need to be declare in throws clause

Please confirm



Under EJB 3, for exception which extends java.lang.Exception class by default is confirmed as an Application Exception no matter you marked it using @ApplicationException.

Confirm that any exceptin extending RuntimeException does not need to be declare in throws clause, BUT any difference for a "RuntimeException which is marked as @ApplicationException" ?


Code Snippet 1:


Code Snippet 2:



Any differences in the above two code snippet , what I confuse is ... is it a MUST to throws/catch ALL application exception no matter it is a "Checked Exception" or "RuntimeException defined using @ApplicationException" ?

So , in Code Snippet 1 , the throws clause does not need to have MyException2.

How bout Code Snippet 2, does the throws clause need to have MyException2 ?
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding is for RuntimeException, it is not required to be mentioned in throws clause whether it is annotated as @ApplicationException or not

Please correct me
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


My understanding is for RuntimeException, it is not required to be mentioned in throws clause whether it is annotated as @ApplicationException or not

Please correct me



I will write some code to test it out in glassfish application server, but might need some time to install my environment in virtual machine ... so might reply you tomorrow.

or you might test it if you have the environment on place and let me know the answer, appreciate ~
 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amirr Rafique wrote:My understanding is for RuntimeException, it is not required to be mentioned in throws clause whether it is annotated as @ApplicationException or not

Please correct me



Right. RuntimeException is not required to be defined in a throws clause.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per section 5.4.17 of EJB 3.0 Core specification, a message-driven bean’s message listener method must not throw the java.rmi.RemoteException. It can throw any other exception. If it throws an application exception marked with rollback, the transaction (if any) is rolled back, the exception is rethrown to the resource adapter, and the bean is NOT discarded. If it throws a system exception, the exception is logged, the transaction (if any) is rolledback, and the bean is discarded.

note: if you wanna let the message listener method of a MDB throw any application-exception (inherited from java.lang.Exception) you must specify the messageListenerInterface via annotion (instead of just let the mdb-class implement MessageListener:
 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if my answer is too late, but it may help someone else. From the Java EE tutoriel:

Your onMessage method should handle all exceptions. It must not throw checked exceptions, and throwing a RuntimeException is considered a programming error.

 
reply
    Bookmark Topic Watch Topic
  • New Topic