Results 1 to 2 of 2
  1. #1
    nicoeschpiko is offline Member
    Join Date
    Dec 2010
    Location
    New Engaland
    Posts
    11
    Rep Power
    0

    Default How to throw exception w/RMI from Server->Client w/o "nested exception..." mentioned

    I have a Server throwing a custom exception defined:

    Java Code:
    package class.project;
    
    import java.rmi.RemoteException;
    
    public class SecurityException extends RemoteException {
    	/**
    	 * Serializable class SecurityException requires declaring a static final serialVersionUID field of type long
    	 */
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * Constructor used to pass msg String argument up to Exception class constructor.
    	 *
    	 * @param msg  String argument 'msg' passed up to Exception class constructor
    	*/
    	public SecurityException(String msg) {
    		super(msg);
    	}
    }
    to the Client via RMI, where the Client attempts to display the SecurityException:
    Java Code:
    	public static void performTestOne(ATM atm) {       
    		try {
    			atm.getBalance(getAccountInfo(0000001, 5555));
    		} catch (Exception e) {
    			System.out.println("Failed as expected: "+e);
    		}
    	}
    And I am wondering how to only display the SecurityException details provided by the Server:
    Java Code:
    if ( !this.security.authenticateTransaction(account_info, account) ) {
    				throw new SecurityException( "Unable to authenticate transaction.  Invalid username/password combination." );
    			}
    Unfortunately I am getting the following output:
    Failed as expected: java.rmi.ServerException: RemoteException occurred in server thread; nested e
    xception is:
    class.project.SecurityException: Unable to authenticate transaction. Invalid username
    /password combination.
    What I need to see is:
    Failed as expected: class.project.SecurityException: Unable to authenticate transaction. Invalid username
    /password combination.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Instead of

    Java Code:
    System.out.println("Failed as expected: "+e);
    try...

    Java Code:
    System.out.println("Failed as expected: "+e.getCause());

    When a remote object throws an exception, it gets bundled into a java.rmi.ServerException as the 'detail' field. Also, you'll have to change your catch statement to specifically catch a ServerException instead of the more generic Exception for this to work.

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 08:42 PM
  2. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  3. Replies: 1
    Last Post: 04-02-2009, 10:50 PM
  4. Replies: 10
    Last Post: 01-29-2009, 09:00 AM
  5. Replies: 6
    Last Post: 08-18-2008, 07:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •