Java異常処理の心得
プロジェクトは社内の一つの開発プラットフォームに基づいています。javaの基本的な処理は全部包装されました。異常処理も特に重要になります。
コードを書く時、私の前の目的はtry catchを要求しないと一般的に処理しないということです。プログラムが正常な状況であれば、問題は間違いなく発生しますが、テストの過程で多くの問題を発見しました。
方法は運転時の異常を自動的に提示しないので、コードを書く時にこれらの異常処理を無視しました。一連の異常な運行状況のもとで問題が発生します。
コードの一部を使って詳しく説明します。
上のコードの中でユーザーIDを取得するとカスタムSession Timeout Exceptionの異常を抛り出します。データを保存する時、すべての異常をカスタムSaveForm Exceptionの異常にまとめました。データを提出するのもすべての異常をカスタムSubmitFormExceptionの異常にまとめました。このように異常があったら正確にエラーメッセージをフロントに伝えてalertを行うことができます。もちろん重要な点があります。このカスタムロールオーバーの異常はよく分かりません。ロールバック異常はプログラムが異常に発生した時にロールバックをトリガした時に出る異常です。前のコードを保存して提出しても、業務を使っています。運行時に異常が発生したら、データベースに対して実行される操作などを行います。Json変換時も運転時の異常を持ち出します。try catchでなくてもヒントはありません。最初は私もこのコードの時はtry catchは全部使えませんでした。エラーがないので、正常運行でも大丈夫です。後で加えたこれらの処理。
カスタム異常の例を挙げると、これはカスタムSession Timeout Exceptionです。
手続き中に事務の処理をする場合、必ず注意してください。事務開始、事務提出、事務ロールは同時に使用します。そうしないとデッドロックになりやすいです。
たとえば:
struts.xml設定ファイルがエラーページのコードにジャンプします。
extedのaction.xmlプロファイル
jspで異常情報ページを設計して、エラー情報を一目で分かります。ajaxを使用してバックグラウンドから投げられたエラーメッセージを呼び出すと、ajax要求に追加されます。
コードを書く時、私の前の目的はtry catchを要求しないと一般的に処理しないということです。プログラムが正常な状況であれば、問題は間違いなく発生しますが、テストの過程で多くの問題を発見しました。
方法は運転時の異常を自動的に提示しないので、コードを書く時にこれらの異常処理を無視しました。一連の異常な運行状況のもとで問題が発生します。
コードの一部を使って詳しく説明します。
try{
// ID
String userId=getUserInfo().getUserId();
String status=bean.getStatus();
//
if("1".equals(status)){ //
evectionExpenseFormService.doSave(bean,detailBean,userId);
}else if("2".equals(status)){ //
//
completeTaskResult=evectionExpenseFormService.doSubmit(bean,getUserInfo(),category,bean.getProcessinsId());
}
// id ,
setResult("result",bean.getId()+"&"+bean.getApplyNo()+"&"+JSONObject.fromObject(completeTaskResult));
} catch (SessionTimeoutException stex) {
//Session
log.error(stex);
//
//ALMOND_MVC_00006 ,
throw AmondException.ThrowAmondException("ALMOND_MVC_00006");
// ,
//this.setErrorMessage("TOFT_MVC_00006");
//return RLOGIN;
} catch (SaveFormException savefex){
//
log.error(savefex);
throw AmondException.ThrowAmondException("ALMOND_MVC_00007");
} catch(UnexpectedRollbackException urex){
//
log.error(urex);
throw AmondException.ThrowAmondException("ALMOND_MVC_00008");
} catch (SubmitFormException subfex){
//
log.error(subfex);
throw AmondException.ThrowAmondException("ALMOND_MVC_00009");
} catch (JSONException jsonex){
//JSON
log.error(jsonex);
throw AmondException.ThrowAmondException("ALMOND_MVC_00010");
} catch (Exception ex){
log.error(ex);
this.setErrorMessage(ex.getMessage());
return ERROR;
}
上のコードの中でユーザーIDを取得するとカスタムSession Timeout Exceptionの異常を抛り出します。データを保存する時、すべての異常をカスタムSaveForm Exceptionの異常にまとめました。データを提出するのもすべての異常をカスタムSubmitFormExceptionの異常にまとめました。このように異常があったら正確にエラーメッセージをフロントに伝えてalertを行うことができます。もちろん重要な点があります。このカスタムロールオーバーの異常はよく分かりません。ロールバック異常はプログラムが異常に発生した時にロールバックをトリガした時に出る異常です。前のコードを保存して提出しても、業務を使っています。運行時に異常が発生したら、データベースに対して実行される操作などを行います。Json変換時も運転時の異常を持ち出します。try catchでなくてもヒントはありません。最初は私もこのコードの時はtry catchは全部使えませんでした。エラーがないので、正常運行でも大丈夫です。後で加えたこれらの処理。
カスタム異常の例を挙げると、これはカスタムSession Timeout Exceptionです。
public class SessionTimeoutException extends Exception{
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = 5721686959716896576L;
public SessionTimeoutException(){
}
public SessionTimeoutException(Throwable cause){
super(cause);
}
public SessionTimeoutException(String message){
super(message);
}
}
手続き中に事務の処理をする場合、必ず注意してください。事務開始、事務提出、事務ロールは同時に使用します。そうしないとデッドロックになりやすいです。
たとえば:
try{
beginTransaction();//
// ,
if(processinstancesid != null && !"".equalsIgnoreCase(processinstancesid)) {
//
} else {//
}
commitTransaction();//
}catch(Exception ex){
rollbackTransaction();//
throw new SubmitFormException(ex);
}
struts.xml設定ファイルがエラーページのコードにジャンプします。
<package name="finance" extend="toft-default" namespace="/finance">
<action name="individualFormList" class="finance.IndividualFormListAction">
<result name="success">/almond/finance/success.jsp</result>
</action>
</package>
extedのaction.xmlプロファイル
<package name="toft-default" namespace="default">
<result-types>
<result-type name="jsp"
class="com.mvc.result.supports.ServletResult" />
<result-type name="ajax"
class="com.mvc.result.supports.AjaxResult" />
<result-type name="redirect"
class="com.mvc.result.supports.RedirectResult" />
</result-types>
<interceptors>
<interceptor name="exceptionInterceptor"
class="com.mvc.interceptor.supports.ExceptionInterceptor">
</interceptor>
<interceptor-stack name="interceptor-stack">
<interceptor-ref name="exceptionInterceptor" />
</interceptor-stack>
</interceptors>
<global-results>
<!-- failure -->
<result name="failure">/almond/exception/failure.jsp</result>
<!-- error -->
<result name="error">/almond/exception/error.jsp</result>
<!-- alert -->
<result name="alert">/almond/exception/alert.jsp</result>
<!-- message -->
<result name="message">/almond/exception/message.jsp</result>
</global-results>
<default-interceptor-ref>interceptor-stack</default-interceptor-ref>
<default-result-type>jsp</default-result-type>
</package>
Exception Interceptor.javaスクリーンクラスでエラーメッセージをオブジェクトに設定します。戻るタイプのパッケージによるエラー情報処理は、例えば、<result-type name=“ajax”のようなものです。 class=「comp.toft.mvc.resupports.AjaxResult」/> jspで異常情報ページを設計して、エラー情報を一目で分かります。ajaxを使用してバックグラウンドから投げられたエラーメッセージを呼び出すと、ajax要求に追加されます。
error: function(request, textStatus){
toft.unlockPage();
alert(request.responseText);
}
エラーメッセージをページに表示します。