Errorcode. Java simple test code. Specific application logic: manually throw exception information and roll back and forth the transaction logic based on the error code.
Public Enum errorcode {// system level success ("000000", "success"), sys_error ("999999", "system exception"), failed ("900000", "operation failed! "), // Transaction part owner_not_exist (" 500002 "," user (owner) does not exist "),; private string code; private string text; private errorcode (string code, string text) {This. code = Code; this. TEXT = text;} Public String getcode () {return code;} public void setcode (string code) {This. code = Code;} Public String gettext () {return text;} public void settext (string text) {This. TEXT = text;} public static void main (string [] ARGs) {errorcode error = errorcode. failed; try {error = testfun (100); // test method.} Catch (exception e) {system. err. println ("E:" + E); string errormsg = E. getmessage (); system. err. println ("errormsg:" + errormsg); system. err. println ("errormsg2:" + E. getlocalizedmessage (); If (errormsg! = NULL) {string [] MSG = errormsg. Split (","); If (MSG! = NULL & MSG. length> 0) {errorcode. sys_error.setcode (MSG [0]); errorcode. sys_error.settext (MSG [1]) ;}// the error code error = errorcode is returned. sys_error; system. err. println ("Code:" + error. getcode (); system. err. println ("text:" + error. gettext () ;}} Private Static errorcode testfun (int I) throws exception {system. err. println ("received parameter value:" + I); errorcode error = errorcode. owner_not_exist; string errormsg = Error. getcode () + "," + error. gettext (); // return exception information. Throw new exception (errormsg); // manually throw an exception. // Throw new exception (); // throw an exception manually. // Return NULL ;}}
The error code returned by the errorcode Enumeration type is tested. An exception message is thrown manually to roll back and forth transactions based on the error code.