Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Exception

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The knights of nee want a shrubbery. And a tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic