ページの先頭行へ戻る
Interstage Application Server V12.3.0 アプリケーション作成ガイド(CORBAサービス編)
FUJITSU Software

4.2.2 サーバアプリケーションの例外処理

(1)例外情報の設定

IDLコンパイラで、インタフェース名::例外の識別名の例外クラス名が生成されます。このクラスでは、例外メンバをprivateデータとして保持します。この例では、ODdemo、calculator、ZEROPARAMがそれにあたります。例外情報を設定するためには、まず、例外クラスのインスタンスをC++の演算子newで生成し、続いて例外クラスのメンバにデータを設定します。最後に、CORBA::Environment::exception()を呼び出して、Environmentクラスのインスタンスにユーザ例外を設定します。

ODdemo::calculator::result             // 復帰値
ODdemo_calculator_impl::calculate(     // divideメソッドの実装
      CORBA::Long     a, 
      CORBA::Long     b, 
      CORBA::Environment & ) 
      throw( CORBA::Exception ) 
{
        : 
    if( b == 0 ){ 
        // 例外クラスをthrowする。
        ODdemo::calculator::ZEROPARAM  exc;
        throw( exc ); 
    }
        : 

    return res; 
}

注意

サーバメソッド内部から他のメソッドを呼び出す場合の引数として、サーバメソッドの引数に渡されるCORBA::Environmentクラスのインスタンスを使用しないでください。サーバメソッドの例外情報に、他のメソッドで発生したサーバメソッド内部の例外情報が設定されることになります。サーバメソッド内部から他のメソッドを呼び出す場合は、別のCORBA::Environmentクラスのインスタンスを使用してください。
以下に正しいプログラム例、および誤ったプログラム例を示します。

[正しいプログラム例]
ODdemo::calculator::result
ODdemo_calculator_impl::calculate(
    CORBA::Long        a, 
    CORBA::Long        b, 
    CORBA::Environment &env ) 
    throw( CORBA::Exception ) 
{
    CosNaming::NamingContext_ptr  nc; 
    CosNaming::Name               name; 
    CORBA::Object_ptr             obj; 
    CORBA::Environment            local_env; 
                                /* 別のCORBA::Environmentクラスのインスタンスを用意 */
        :
    
    /* 
     * CosNaming::NamingContext::bind()の例外情報は、local_envに設定されるため、
     * ODdemo::calculator_impl::calculate()の例外情報として扱われることはない
     */
    nc->bind( name, obj, local_env ); 
        :
}

[誤ったプログラム例]
ODdemo::calculator::result
ODdemo_calculator_impl::calculate(
    CORBA::Long        a, 
    CORBA::Long        b, 
    CORBA::Environment &env ) 
    throw( CORBA::Exception ) 
{
    CosNaming::NamingContext_ptr  nc; 
    CosNaming::Name               name; 
    CORBA::Object_ptr             obj; 
        :
      
    /* 
     * CosNaming::NamingContext::bind()が例外復帰した場合、
     * CosNaming::NamingContext::bind()の例外情報が
     * ODdemo::calculator_impl::calculate()の例外情報として扱われてしまう
     */
    nc->bind( name, obj, env ); 
        :
}

(2)例外情報の獲得

サーバアプリケーションで例外を獲得する方法は、クライアントアプリケーションの例外処理と同様です。詳細については、“4.5.2 クライアントアプリケーションの例外処理”を参照してください。