5.2.5 システム例外への例外のダウンキャスト

キャッチした例外をSystemExceptionへダウンキャストするために,アカウントのクライアントプログラムを修正できます。コードサンプル5-7および5-8に,クライアントプログラムの修正方法を示します。コードサンプル5-9および5-10に,システム例外が発生した場合に表示される出力を示します。

コードサンプル5-7 システム例外への例外のダウンキャスト(C++)

int main(int argc, char* const* argv) {
  try {
     // Initialize the ORB.
     CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);

     // Bind to an account.
     Account_var account = Account::_bind();

     // Get the balance of the account.
     CORBA::Float acct_balance = account->balance();
     // Print out the balance.
     cout << "The balance in the account is $"
           << acct_balance << endl;
     } catch(const CORBA::Exception& e) {
        CORBA::SystemException* sys_excep;
        sys_excep = CORBA::SystemException::_downcast
                                   ((CORBA::Exception)(&e));
        if(sys_excep != NULL) {
           cerr << "System Exception occurred:" << endl;
           cerr << "exception name: " <<
           sys_excep->_name() << endl;
           cerr << "minor code: " << sys_excep->minor()
                 << endl;
           cerr << "completion code: "
                 << sys _excep->completed()
                 << endl;
     } else {
        cerr << "Not a system exception" << endl;
        cerr << e << endl;
     }
  }
}

コードサンプル5-8 システム例外への例外のダウンキャスト(Java)

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 an account
       Account account = AccountHelper.bind(orb, "/bank_poa",
                     "BankAccount".getBytes());
       // Get the balance of the account
       float balance = account.balance();
       // Print the account balance
       System.out.println("The account balance is $" + balance);
    }catch(Exception e){
       if (e instanceof org.omg.CORBA.SystemException){
         System.err.println("System Exception occurred:");
       }else {
         System.err.println("Not a system exception");
       }
       System.err.println(e);
    }
  }
}

コードサンプル5-9 システム例外からの出力(C++)

System Exception occurred:
     exception name: CORBA::NO_IMPLEMENT
     minor code: 0
     completion code: 1

コードサンプル5-10 システム例外からの出力(Java)

System Exception occurred:
in thread "main"org.omg.CORBA.OBJECT_NOT_EXIST
                                  minor code: 0 completed: No

<この項の構成>
(1) 特定の型のシステム例外のキャッチ

(1) 特定の型のシステム例外のキャッチ

すべての型の例外をキャッチするのではなく,主要な各型の例外を明確にキャッチするように選択できます。コードサンプル5-11にC++,およびコードサンプル5-12にJavaのそれぞれの方法を示します。

コードサンプル5-11 特定の型の例外のキャッチ(C++)

. . .
int main(int argc,char* const* argv){
  try {
      // Initialize the ORB.
      CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);

      // Bind to an account.
      Account_var account = Account::_bind();

      // Get account balance.
      CORBA::Float acct_balance = account->balance();
      // Print out the balance.
      cout << "The balance in the account is $"
            << acct_balance << endl;
  }
  // Check for system errors
  catch(const CORBA::SystemException& sys_excep) {
     cout << "System Exception occurred:" << endl;
     cout << "exception name: " << sys_excep->_name() << endl;
     cout << "minor code: " << sys_excep->minor() << endl;
     cout << "completion code: " << sys_excep->completed()
           << endl;
  }
}
. . .

コードサンプル5-12 特定の型の例外のキャッチ(Java)

public class Client {
  public static void main(String[ ] args) {
    try {
       org.omg.CORBA.ORB orb =
                        org.omg.CORBA.ORB.init(args, null);
       byte[ ] managerId = "BankManager".getBytes();
       Bank.AccountManager manager =
            Bank.AccountManagerHelper.bind(orb,
                                         "/bank_agent_poa",
                                         managerId);
       String name =
               args.length > 0 ? args [0] : "Jack B. Quick";
       Bank.Account account = manager.open(name);
       float balance = account.balance();
       System.out.println("The balance in " + name +
                          "'s account is $" + balance);
    } catch(org.omg.CORBA.SystemException e) {
       System.err.println("System Exception occurred:");
       System.err.println(e);
    }
  }
}