Java custom error classes

Source: Internet
Author: User

In the program, you need to throw an exception and output the error information on the user interface.

One scenario is to capture exceptions one by one when the UI is displayed in the program, and then display the corresponding ErrorMessage. Sometimes, if the program throws an exception due to business logic, you need to customize the exception.

How to handle exception messages in a centralized manner according to the requirements of multiple languages, these error messages need to be processed in a centralized manner.

Custom error messages.

Code
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;
}
}
}

The error code is thrown here, and the error message of the resource file can be obtained based on the error code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.