• Java 自定义错误类


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

    一种情况是在程序中最后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;
            }
        }
    }


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

  • 相关阅读:
    送股分红是怎么回事?
    基金申购费用计算
    msn 爬楼梯
    沪市证券交易费用
    OS X
    见到郎咸平:)
    Long long time_Blog更新了
    生~气:)
    突然想起去年的今天
    久久沉思以后
  • 原文地址:https://www.cnblogs.com/likwo/p/1791187.html
Copyright © 2020-2023  润新知