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;
}