In the program, an exception needs to be thrown and then the error message is output in the user interface.
One situation is to catch exceptions one by one when the last UI is displayed in the program, and then display the corresponding ErrorMessage. Sometimes, the program needs to throw exceptions due to business logic reasons, and you need to customize exceptions.
How to centrally process exception messages to meet the requirements of multi-language languages? These error messages need to be processed centrally.
Custom error message.
Copy the code code as follows:
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
SYSTEM_ERROR( "99999" ),
//User authentication error
USER_AUTH( "03003" );
private String errorCode;
Type(String errorCode)
{
this.errorCode = errorCode;
}
public String getErrorCode()
{
return this.errorCode;
}
}
}
An error code is thrown here, and then the error message of the resource file can be obtained based on this error code.