Excep - Using CORBA User Exceptions

Description:

CORBA IDL exceptions are mapped into standard C# classes, derived from class 'Ics.CORBA.UserException'. Although an exception is conceptionally a valuetype (i.e. passed to the client 'by value'), there is no need to provide a client side implementation, because the MiddCorIdl compiler will generate the complete class code.

IDL exceptions may be declared within the scope of an interface. According to the J-Integra® Espresso IDL-to-C# mapping, this will result in a separate namespace named '<interface>Package' containing the generated class.
(s.f. also the ListArrayStruc example.)

Source:

\Demo\Excep

Mapping:

CORBA IDL Exception <---> Ics.CORBA.UserException

Example

Step 1. The IDL (Excep.idl):

    module Excep
    {
        interface IGreetings
        {
            exception ExcpName
            {
                string 	strWhy;
                long	lErrorCode;
            };
            
            string hello( in string strName) raises (ExcpName);
        };
    };    

(Will be compiled into Excep.cs)

Step 2. The Server Implementation (ExcepSrv.cs):

    public class Greetings: GreetingsPOA
    {
        public override string hello( string _a_strmyName )
        {
            if( _a_strmyName == "John")
            {
                throw new Excep.IGreetingsPackage.ExcpName("John was not friendly", 1001);
            }
            System.Console.WriteLine("Function 'Hello' Parameter '{0}'", _a_strmyName);
            return "Greetings to " + _a_strmyName;
        }
    }

Note, that the C# implementation for the declared exception is located within a generated namespace 'Excep.IGreetingsPackage' (from Excep.cs):

    namespace Excep
    {
        namespace IGreetingsPackage
        {
            public class ExcpName: Ics.CORBA.UserException		
            {
                public string strWhy;
                public int lErrorCode;
                public ExcpName()
                {
                }
                public ExcpName( string strWhy, int lErrorCode )
                {
                    this.strWhy = strWhy;
                    this.lErrorCode = lErrorCode;
                }
                public ExcpName( string _reason, string strWhy, int lErrorCode )
                {
                    this.strWhy = strWhy;
                    this.lErrorCode = lErrorCode;
                }
            }
        }
    }

Step 3. The Client (ExcepClt.cs):

The client will catch the exception from the remote CORBA server just like any other exception:

    try
    {
        // this should work well:
        System.Console.WriteLine("{0}", m_oIGreetings.hello("Hans"));
        
        // this should result in an 'Excep.IGreetingsPackage.ExcpName' exception: 
        System.Console.WriteLine("{0}", m_oIGreetings.hello("John"));
    }
    catch( Excep.IGreetingsPackage.ExcpName ex)
    {
        System.Console.WriteLine("Exception from Server. Reason:'{0}', ErrorCode:{1}",
                                 ex.strWhy, 
                                 ex.lErrorCode);
    }

Step 4. Run the example

a.) Start the Server.

b.) Start the Client.