java specification exception and error codes and definitions used

Foreword

  • This essay is some of the lessons learned during recent actual project development, the need for a partner learn.

 

Scenes

  • Java use to provide services back-end systems, the user (possibly web front end or a third party caller) called by api form.

 

Previous issues you may encounter

  • Will exception here? If this happens, how should I do?
  • I would like to define which custom exception?
  • When capturing to an "unknown" exception, I will throw it to go or place print its stack information?

 

problem analysis

  • Why would an exception?
      Simple to understand, the abnormal state is expected during the program execution state fails to perform.
  • Why custom exception?
      1) system (referred to herein as non of this project) abnormal do differentiate, so that we can fight for different exceptions do different things.
      2) custom exception is a custom object, you may carry some additional information.
  • What should be done after abnormal generation?
      1) Print exception stack: In this case, the general operation and maintenance needs or developers locate and manually resolve the problem, because the stack is printed on posters, or else it will not be printed
      2) the execution candidate programs (may also need to Print stack)

 

Abnormal classification

  • A collection of projects and business logic of the possible exceptions do classification. Whether the system is doing, we can be divided into "abnormalities" and "non-system abnormalities," if that is too general and they both can be broken down (in the end you may find there is no need subdivision).
      System abnormalities: Typically, such exceptions are generally prompt the consumer system is unavailable while requiring more personnel into treated
      non-system exceptions: after some obligation, such exceptions are logical only, such as checking the user parameter error thrown abnormal
  • Abnormal classification principles: first, a clear definition of what a good system is a system abnormality, what kind of exceptions within custom exception when to capture an exception or error, this exception is to see what kind of scene, and then classified.
  • Classification of abnormal processing. Since we throw an exception, it would have to be captured treatment, if your system is well-designed, then the general situation is abnormal unified capture, in which case we may first capture a custom exception, then capture the maximum anomaly, due to the general custom exception already carries relevant error message, so we can easily control returns to the caller, so be the biggest fight of abnormal define a default return value.

 

Correctly deal with third-party tools (dependent) throws an exception

  • After the above definition, the exception may have their own code generation for a better solution, but when captured exceptions thrown by someone, probably at a loss (the beginning I was so), this time to go back and look perhaps just described will be more clear, throw an exception if the code is not dependent on us, but part of our project, then you must also know exactly how this anomaly belongs to a level in our system. From the relations point of view, we are relying party, they were relying party can not rely on the use defined above the anomaly.
  • So when we Exception caught a relying party, need to determine the abnormal level should correspond to which exception has been defined in our system, and then converted to an exception for our own definition, here to note that if this exception is the need to print error stack, then we must not throw away the original anomaly directly, it is best to be as nested exceptions contained in our custom exception and then throw it , which is equivalent to the relying party thrown exception paste on a label, so that we can capture a unified process.

 

With the limited exception handling duties in the midst of endless possibilities

  The problem, in fact we are using, and that is wrong with the code to use. And define an exception to the same class, and when to use the definition of error codes should be classified defined, to avoid duplication and confusion define use. And in the end you will find that most of the error codes defined error code is mostly business, that is, the error code represents a clear error code, and that this error is described to the caller to see, and system-level exception detail information has been printed out in the form of a stack in the log, and that a different definition of a more wrong error code is also of little significance. Of course, except if the error code to pass the case on the classification of statistical anomaly, but if a system has to this point, then basically you can choose the other options.

 

Exception class definitions and exemplary error code

demand

  • The type of the abnormal system is abstract and defines the corresponding exception.
  • Custom error objects can carry related error messages in order to capture the reunification process
  • The need to ensure the error code does not use the confusion, the "system-level" category error code can only be used for "System" category exceptions, can not use the confusion

 

Sample Code

public  interface Error Code { 
  String getErrorCode (); 
  String getErrorDesc (); 
}
Gets the error code and its contempt for the interface definition information
/ ** 
 * enumerate user exception error code 
 * / 
public  enum UserErrorCode the implements ErrorCode {
   / ** 
   * username error 
   * / 
  USERNAME_ERR ( "ERR-username", "user name error" ),
   / ** 
   * Password error 
   * / 
  PASSWORD_ERR ( "ERR-pASSWORD", "password error" ), 
  ; 

  UserErrorCode (errorCode String, String errorDesc) { 
    the this .errorCode = errorCode;
     the this .errorDesc = errorDesc; 
  } 

  // error code. 
  @Getter
   Private String errorCode;
   // corresponding external error code description information. 
  @Getter
  private String errorDesc;
}
User exception error codes enumerated example
/ ** 
 * abnormalities enumeration error code 
 * / 
public  enum SystemErrorCode the implements the ErrorCode {
   / ** 
   * Internal error 
   * / 
  SYSTEM_ERR ( "ERR-System", "Internal error" ), 
  ; 

  SystemErrorCode (errorCode String, String errorDesc ) { 
    the this .errorCode = errorCode;
     the this .errorDesc = errorDesc; 
  } 

  @Getter 
  Private String errorCode; 
  @Getter 
  Private String errorDesc; 
}
System exception error codes enumerated example

Import lombok.Getter; 

public  class a UserException the extends a RuntimeException the implements the ErrorCode { 

  @Getter 
  Private String errorCode; 
  @Getter 
  Private String errorDesc; 

  / ** 
   * @param error code type errorCode enumerated, which contains the error code and the error code corresponding to the description information.
    * / 
  public a UserException (UserErrorCode errorCode) {
     Super ();
     the this .errorCode = errorCode.getErrorCode ();
     the this .errorDesc = errorCode.getErrorDesc (); 
  } 

  / ** 
   * @paramerrorCode enumerated type error code, which contains the error code and the corresponding error code description information. 
   * @param Message detailed description is preferably customized for the play log, and may be { @link the ErrorCode # getErrorDesc ( )} the same
    * / 
  public a UserException (UserErrorCode errorCode, String Message) {
     Super (Message);
     the this .errorCode = errorCode.getErrorCode ();
     the this .errorDesc = errorCode.getErrorDesc (); 
  } 

  / ** 
   * @param errorCode enumeration error code type, which contains the error code and the corresponding error code description information. 
   * @param the cause is nested stack error
    * / 
  public a UserException (UserErrorCode errorCode, the cause is the Throwable) {
     Super(the cause is);
     the this .errorCode = errorCode.getErrorCode ();
     the this .errorDesc = errorCode.getErrorDesc (); 
  } 

  / ** 
   * @param error code type errorCode enumerated, which contains the error code and the error code corresponding to the description information. 
   * @param message from the detailed description is best defined, for playing the log, and may be { @link the errorCode # getErrorDesc ()} same 
   * @param the cause is nested stack error
    * / 
  public a UserException (UserErrorCode errorCode , String Message, the cause is the Throwable) {
     Super (Message, the cause is);
     the this .errorCode = errorCode.getErrorCode ();
     the this .errorDesc = errorCode.getErrorDesc();
  }
}
Custom configurations including various methods abnormality

ps: If you read this definition Constructor for exception class, then you must know how to use the error code defines the enumeration, as long as not a string on the line, otherwise it will default and official conflict, the official default string represents the descriptive information that wrong, and we must not only take advantage of characterization information can be printed simultaneously in the stack, we also need to describe the error code and error code to pass information (information described here is generally transmitted to the caller , not to hit the log so that these two descriptions are generally not the same), a plurality of incoming information, so the object is our best choice.

 

If deficiencies welcome message exchange.

Guess you like

Origin www.cnblogs.com/laeni/p/11265101.html