Custom Exception

Step 1: Define an Exception parent class
public class BaseException extends Exception
{
    private static final long serialVersionUID = 1L;
    //Exception type Get different exception information according to the returned digital information
    protected int exceptionKey;
    //Exception information
    protected String message;

    protected Object object;   
   
    public Object getObject()
   {
       return object;
   }

   public void setObject(Object object)
   {
       this.object = object;
   }

    public String getMessage()
   {
        return message;
   }

   public void setMessage(String message)
   {
this.message = message;
   }

    public int getExceptionKey()
    {
        return this.exceptionKey;
    }

public BaseException()
    {
    }

    public BaseException(int exceptionKey)
    {
        this.exceptionKey = exceptionKey;
    }
public BaseException(String message)
    {
        this.message = message;
    }
}
Step 2: Then customize your own exception class, inherit the above Exception parent class
     public class LadingsException extends BaseException
{
static final long serialVersionUID = 1001;

final public static int UserDefined = 10000; //User-defined error

/**  LadingsException  */
final public static int OutQuantity = 1; //超出数量
final public static int OutWeight = 2; //超出重量
final public static int OutArea = 3; //超出面积
final public static int OutVolume = 4;//超出体积

public LadingsException(int exceptionKey)
{
super(exceptionKey);
}

public LadingsException(String message)
{
super(message);
this.exceptionKey = UserDefined;
}

public String toString()
{
return exceptionKeyToMessage();
}

public String exceptionKeyToMessage()
{
if (exceptionKey == UserDefined)
{
return message;
}
if (exceptionKey == OutQuantity)
{
return "Insufficient quantity to be allocated";
}

if (exceptionKey == OutArea)
{
return "Insufficient area to be allocated";
}

if (exceptionKey == OutVolume)
{
return "To be allocated Insufficient volume";
}

if (exceptionKey == OutWeight)
{
return "Insufficient weight to be allocated";
}

return "System internal error";
}

public String getMessage()
{
return exceptionKeyToMessage();
}
}
The third step uses a custom exception , in method A through throws LadingsException, thrown to the previous level
    public void A ()throws LadingsException{
          //Call method B
          B();
   }
The fourth step is to use throw new LadingsException (parameter) in method B, and throw it into A
   public void B () throws LadingsException{
         //Determine which kind of exception belongs, and then pass the LadingsException exception class
          // Constructor
          to   generate
          exception information Go to the upper level C of A, and then pass            try{            A();          }          catch(LaidngsException e){               //Get exception information through e.getMessage() in C. The sixth step is to get the exception in method C Information, which can be sent to the front desk by means of Json, for example, to prompt the user why the operation cannot be used







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326523355&siteId=291194637