Forums Register Login

Application Exception in MDB

+Pie Number of slices to send: Send
Hello

Can an MDB throw an application exception ?

Thanks
+Pie Number of slices to send: Send
any help on above post?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
EJB Core 14.3.4

MDB method throw Application Exception
and container re-throw Application Exception to resource adapter
+Pie Number of slices to send: Send
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 ?



+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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 ?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 


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 ~
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
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.

 
It was the best of times. It was the worst of times. It was a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 7453 times.
Similar Threads
How to stop MDB retry invocation during System Exception
Error deploying message driven bean
Query about MDB throwing application exception
MDB ejbCreate And createException
system exceptions
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:07:44.