Main page / CORBA with MinCor.NET page

Excep - Using CORBA User Exceptions


Description: 
	CORBA IDL exceptions are mapped into standard C# classes, derived from class 
	'Middsol.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 MinCor IDL compiler will generate
	the complete class code.
	
	IDL exceptions may be declared within the scope of an interface. According to the 
	MinCor.NET 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: 
	MinCor\Demo\Excep

Mapping: 
	CORBA IDL Exception  <--->  Middsol.CORBA.UserException



Example 

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)


The Implementation (ServerImpl.cs):


    
    public class  GreetingsImpl: Excep.IGreetingsPOA
    {
        private ServerImpl m_oServer;
 
        public GreetingsImpl( ServerImpl a_oServer)
        {
            m_oServer = a_oServer;
        }
    
        public override string hello(  string a_strmyName  )
        {
            if( a_strmyName == "John")
            {
                m_oServer.theFrm.writeLog(  "Function 'Hello' Parameter '" + a_strmyName +"'. Throw Exception");
                throw new Excep.IGreetingsPackage.ExcpName("John was not friendly", 1001);
            }

            m_oServer.theFrm.writeLog(  "Function 'Hello' Parameter '" + 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: Middsol.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;
                }
            }
        }
    }
    
    
The Client (ClientImpl.cs):

    The client will catch the exception from the remote CORBA server just like
    any other exception:
    
    try
    {
        // this should work well:
        srLog += "\nCall function hello.\n    Returns:" + oIGreetings.hello("Hans");
        
        // this should result in an 'Excep.IGreetingsPackage.ExcpName' exception: 
        srLog += "\nCall function hello.\n    Returns:" + oIGreetings.hello("John");
    }
    catch( Excep.IGreetingsPackage.ExcpName ex)
    {
        srLog += "\nException from Server.\n   Reason:'" + ex.strWhy+ "', ErrorCode:" + ex.lErrorCode;
    }