Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

why Exception block is calling instead of EJBException block

i am calling one method from controller to ejb.in the EJB addEmployee if flag is true i am throwing exception saying that "match not found" and intrun it will call EJB catch block and then it should call controller EJBException block but it is calling Exception block;why it so??

controller
********

public void addEmployee()
{

      try
      {
            remote.addEmployee();
      }
      catch(EJBException e)
      {
            System.out.println("match not found");
      }
      catch(Exception e)
      {

      }


}

EJB
*****
public void addEmployee() thows EJBException
{

      try
      {
      
               //SOME BUSINESS PRCOESS;

             if(flag)
             throw new EJBException("match not found")



      }
      catch(EJBException e)
      {
                  throw new EJBException("match not found")
      }
      catch(Exception e)
      {
           
      }


}


Avatar of Mick Barry
Mick Barry
Flag of Australia image

Probably because the exception you throw is not a EJBException or a subclass of it.
Avatar of chaitu chaitu

ASKER

in EJB it is calling throw new EJBException("match not found") but in controller it is not calling EJBException block..
tell me one thing can i write like this

if(flag)
            throw new EJBException("match not found")
> tell me one thing can i write like this

you can

Check the type of the exception. If you're calling a remote object then its probably getting wrapped in something like a RemoteException

this is remote method.
public java.util.Hashtable addEmployee(Employe emp)      throws RemoteException;

so how can i avoid this??
so now i watn to throw EJBException in the method so now i changed method to

public java.util.Hashtable addEmployee(Employe emp)      throws RemoteException,EJBException ;
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry it retruns void ;;
catch the RemoteException means i should use the getCause() method  in catch(Exception e) block;;
if i write my own userdefined exception also same thing is happening
    catch(RemoteException e)
     {
          Throable cause = e.getCause();
           
     }
Any kinds of exceptions thrown remotely will be wrapped up into remote exceptions and thrown by the container, I guess.
if i write my own checked exception then its working fine;;
>> if i write my own userdefined exception also same thing is happening
>> if i write my own checked exception then its working fine;;

What's the difference b/w these two?
in my first post i have made a mistake now its working fine;