/*
 * This class demonstrates the mapping of Exceptions
 * from IDL to Java.  It will also demonstrate a runtime
 * exception begin thrown
 */

import java.io.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;


public class Client {

    public static void main(String[] args) {
	try {
		//Initialize the ORB
		org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

		//Bind to the Implementation
                String ior;
                if( args.length == 1 ) {
                    // get object reference from command-line argument
                    ior = args[0] ;
                }
                else {
                    // get object reference from file
                    ior = new BufferedReader( new FileReader("ior")).readLine();
                }

                org.omg.CORBA.Object obj = orb.string_to_object( ior );

                // and narrow it to Exceptions.ExceptionTest
                Exceptions.ExceptionTest et = 
			Exceptions.ExceptionTestHelper.narrow( obj );

                // check if stringified IOR is of the right type
                if( et == null ) {
                    System.err.println("stringified IOR is not of right type");
                    System.exit( 1 );
                }

	        //Now, we will call the method we know throws 
		//our user-defined exception
		try {
			System.out.println("Call the method we know will throw a user exception");
			et.throwAUserException();
		}
		catch(Exceptions.UserException1 ex1) {
			System.out.println("Caught the UserException with reason " + ex1.reason + "; error code: " + ex1.code);
		}

		//Now call a method that does NOT declare that it throws
		//an exception.  What we will do is cause a runtime
		//exception on the server, which will result in a runtime
		//exception on this client.  Although the method dont declare 
		//it throws an exception, it is none the less valid to wrap
			//this method call in a try/catch block
		try {
			System.out.println("Call the method we know will cause a runtime exception");
			et.throwARuntimeException();

		}
		catch(org.omg.CORBA.SystemException cex) {
			System.out.println("Caught a system exception " + cex.toString());
			// cex.printStackTrace();
		}

	}
	catch(Exception ex) {
		ex.printStackTrace();
	}
    }
}

