aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Prashanth Bhanu
Ranch Hand

Joined: Sep 25, 2003
Posts: 110
Hi Friends,
Hows can i throw an Exception from a DBClass to a session facade?Say Exception is like IllegalArgumnet.How this can be handled.Since i'm new to the J2EE enviornment this looks complicated to me.Plz help me in this regard
THANX in advance
prashu
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Declare the method as throws Exception(Any type of exception) like

public void method() throws SomeException{

//processing
}

so when any exception will be raised the method will throw it to the calling code.
Prashanth Bhanu
Ranch Hand

Joined: Sep 25, 2003
Posts: 110
Hi Ali,
can u plz eloborate ur answer and is appreciable if u produce it with the example code.I want to know how do u handle the Thrown exception in a calling method of the Exception thrown method!
thanx in advance
prashu
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
It depends what type of processing you want to do when the exception will be thrown. As i have mentioned in your DB Class's method which is expected to raise exception, throw the exception i.e. declare method as throws Exception.

When you call the method catch the exception and do the required processing.

public void someMethod() throws SomeException{

try{
//processing
}catch(SomeException se){
System.out.println("Exception Occured : " + se.getMessage());
throw se;
}


Now you call the method

public void callingMethod(){
try{
someobject.someMethod();
//process in case of success
}catch(SomeException se){
System.out.println("Exception is thrown by someMethod");
//do the processing in case of exception
}
}
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Furthermore, you have to decide how the exception will be handled by the session bean.

1. If it is a RuntimeException like IllegalArgumentException, do nothing - just let it propogate to the container.

2. If it is a exception such as SQLException which the client is not expecting, then this sort of exception must be encapsulated in a javax.ejb.EJBException and thrown to the container.

In both these cases, the container will:

Throw a system exception (java.rmi.RemoteException to a remote client or javax.ejb.EJBException to a local client)
Log the exception
Rollback the transaction
Kill the bean instance

3, If it is an exception that the client expects, ie can recover from it, then the bean must throw a (checked) application exception to the container.

If the bean receives an application exception in the first place, then it can be ducked (or handled and rethrown to the container).

If the bean does not receive an application exception, then the bean must create an application exception and throw it to the container.

And remember, both the bean class and the client interface must declare the application exception.

4. If you have an application exception and the transaction cannot continue, then setRollbackOnly() must be called to prevent the transaction from being committed before the application exception is thrown to the container.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Exception