博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在程序中,需要抛出异常,然后在用户界面进行错误信息输出。

一种情况是在程序中最后UI显示的时候一个一个异常捕获,然后 显示对应的ErrorMessage,有时候,程序因为业务逻辑的原因需要抛出异常,就需要自定义异常。

如何将异常消息集中处理,以对应多语言话的要求 ,这些错误消息就需要集中处理了。

自定义错误消息。

代码

public class MyException extends Exception
{
    
private static final long serialVersionUID = 1L;
    
private Type type;
    
    
public MyException( Type type )
    {
        
super();
        
this.type = type;
    }

    
public MyException( Throwable t, Type type )
    {
        
super( t );
        
this.type = type;
    }

    
public String toString() {
        
return super.toString() + "<" + getErrorType().getErrorCode() + ">";
    }
    
    
public Type getErrorType()
    {
        
return type;
    }
    
    
public enum Type
    {
        
// 系统错误
        SYSTEM_ERROR( "99999" ),
       
        
// 用户认证错误
        USER_AUTH( "03003" );
        
        
private String errorCode;

        Type( String errorCode )
        {
            
this.errorCode = errorCode;
        }

        
public String getErrorCode()
        {
            
return this.errorCode;
        }
    }
}


在这里抛出错误代码,然后可以根据这个错误代码取得资源文件的错误消息。

posted on 2010-08-03 14:07  Likwo  阅读(2691)  评论(0编辑  收藏  举报