EJB 例外を処理する
このトピックでは、EJB コントロールの対象となっている EJB によって送出される例外を処理する方法について説明します。
EJB コントロールについては、EJB コントロール : Web サービスからエンタープライズ JavaBean を使用するを参照してください。
WebLogic Workshop のコントロールの詳細については、コントロール : Web サービスからリソースを使用するを参照してください。
EJB コントロールの対象の EJB が例外を送出した場合、実際の例外と weblogic.rmi.extensions.RemoteRuntimeException を try-catch ブロックで捕捉する必要があります。送出された実際の例外は捕捉されませんが、コンパイラ用に catch ブロックを指定する必要があります。
前述の手法を次の例に示します。
AccountEJB.jar では、引き出し金額が残高よりも多い場合に ProcessingErrorException を送出する withdraw(Double amount) メソッドを定義します。
/** * @jws:control */ private AccountEJBControl account;
/** * Returns from the EJB the new account balance of the account id after a withdrawal. * * @return A String account balance, or the text of any exception that occurs. * * @jws:operation */ public String withdraw(String accountKey, double withdrawAmount) { try { return String.valueOf(account.findByPrimaryKey(accountKey).withdraw(withdrawAmount)); } // catch the reuntime exception catch (weblogic.rmi.extensions.RemoteRuntimeException rre) { // find the real exception Throwable t = rre.getNestedException(); if (t instanceof ProcessingErrorException) { // perform the exception processing return t.getLocalizedMessage(); } else { // re-throw any other exceptions. throw rre; } } catch (FinderException fe) { return fe.getLocalizedMessage(); } catch (RemoteException re) { return re.getLocalizedMessage(); } catch (ProcessingErrorException pe) { // this keeps the compiler happy and will not execute. return pe.getLocalizedMessage(); } }