Java Custom error Class sample code _java

Source: Internet
Author: User

In your program, you need to throw an exception and then output the error message in the user interface.

One scenario is to capture an exception when the final UI is displayed in the program, and then display the corresponding errormessage, and sometimes the program needs to customize the exception because it needs to throw an exception for business logic reasons.

How to centralize exception messages to correspond to the requirements of multiple languages, and these error messages need to be handled centrally.

Custom error messages.

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

Throw the error code here, and then you can get the error message for the resource file based on this 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.