キャッチした例外をSystemExceptionへダウンキャストするために,アカウントのクライアントプログラムを修正できます。コードサンプル5-7および5-8に,クライアントプログラムの修正方法を示します。コードサンプル5-9および5-10に,システム例外が発生した場合に表示される出力を示します。
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;
}
}
}
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);
}
}
}
System Exception occurred:
exception name: CORBA::NO_IMPLEMENT
minor code: 0
completion code: 1
System Exception occurred:
in thread "main"org.omg.CORBA.OBJECT_NOT_EXIST
minor code: 0 completed: No
すべての型の例外をキャッチするのではなく,主要な各型の例外を明確にキャッチするように選択できます。コードサンプル5-11にC++,およびコードサンプル5-12にJavaのそれぞれの方法を示します。
. . .
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;
}
}
. . .
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);
}
}
}