zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }


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

  • 相关阅读:
    golang基础之第一个go程序
    golang基础之工程结构
    golang基础之初识
    Prometheus神器之监控K8s集群
    Kubernetes使用GlusterFS实现数据持久化
    Go使用Makefile构建
    快排的2种分区图解
    密钥交换之DH算法
    go mod使用
    socket常见选项之SO_REUSEADDR,SO_REUSEPORT
  • 原文地址:https://www.cnblogs.com/likwo/p/1791187.html
Copyright © 2011-2022 走看看