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.

Source: Internet
Author: User

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.

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.