Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of java using enumerations to encapsulate error codes and error messages

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you the content of sample analysis of java using enumerations to encapsulate error codes and error messages. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Enumeration encapsulates error codes and error messages

It is convenient to use enumerated types to encapsulate error codes and error messages needed in project.

The usage is package com.dear.simpler.dbservice / * @ author lixiang * Enum types are used to encapsulate exception codes and exception information * * / public enum DBServiceError {/ / RPC layer calls error codes DB_SERVICE_OK (20100, "Service is normal"), DB_SERVICE_DBDAO_ERROR (20104, "return specific exception information of database"), DB_SERVICE_SPEAKER_NOT_EXISTED (20108) "No speaker to query in the database"), DB_SERVICE_SPEAKER_HAVE_NOT_VP (20109, "No corresponding voiceprint in speaker"), DB_SERVICE_UNKNOWN_ERROR (20101, "unknown exception"), DB_SERVICE_AGENT_ERROR (20102, "DBServiceAgent exception"), DB_SERVICE_NETWORK_ERROR (20103, "Network exception") DB_SERVICE_INVALID_FUNCTION (20105, "method name does not exist"), DB_SERVICE_INVALID_PARAMETER (20106, "method parameter error"), DB_SERVICE_FUNCTION_NO_ACCESS (20107, "No access to this method") Private String msg; private int code; private DBServiceError (int code,String msg) {this.code=code; this.msg=msg;} public String getMsg () {return this.msg;} public int getCode () {return this.code;}}

When in use:

DBServiceError.DB_SERVICE_NETWORK_ERROR.getCode ()

DBServiceError.DB_SERVICE_NETWORK_ERROR.getMsg ()

Encapsulate the returned result class and enumerate error codes enumerate error codes / * * enumerate error codes * / public emum ErrorCode {FAILED (601, "Operation failed"), AUTH_ERROR (401, "Authentication failure"), SYS_ERROR (500, "system error"), PARAM_ERROR (400, "Parameter error"), UNKNOWN_ERROR (499, "unknown error"); private int code; private String message Private ErrorCode (int code, String message) {this.code=code; this.message=message;} public String getMessage () {return this.message;} public int getCode () {return this.code;}} encapsulate the returned result class / * return result class * / public class JsonResult {private int status = 0; private String msg = "success"; private Object data / / several common methods public JsonResult success () {return new JsonResult ();} public JsonResult success (Object data) {return new JsonResult (data);} public JsonResult error (ErrorCode errorCode) {return new JsonResult (errorCode.getCode (), errorCode.getMessage ()) } public JsonResult error (int state, String message) {return new JsonResult (state, message);} public JsonResult error (int state, String message, Object data) {return new JsonResult (state, message, data) } / / several construction methods public JsonResult () {} public JsonResult (Object data) {this.data = data;} public JsonResult (Integer state, String message) {this.state = state; this.message = message } public JsonResult (Integer state, String message, Object data) {this.state = state; this.message = message; this.data = data;} public Integer getStatus () {return status;} public void setStatus (Integer status) {this.status = status;} public String getMsg () {return msg;} public void setMsg (String msg) {this.msg = msg } public Object getData () {return data;} public void setData (Object data) {this.data = data;}} Thank you for reading! This is the end of this article on "example analysis of java using enumerations to encapsulate error codes and error messages". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report