How do I throw an exception in the service method and return the status or checksum result? __ Exception and return

Source: Internet
Author: User
In the service method, if we do a transaction by throwing an exception (in fact there is a better way: Spring transaction configuration), then our service method needs to throw an exception, but there is a problem that has not been taken into account when testing the interface today, Problem: When the inspection failed, we set a key to the map message and returned the map, but did not throw an exception, causing the transaction not to be rolled back. To solve this problem, I need to throw an exception when the checksum fails, instead of returning the map object, and since the exception is thrown, how do I return the checksum result? I've tried the following 3 solutions, where scenario 1 is not. 1, using Try{}catch (e) {}finally{} structure to achieveThrow the exception e back in the catch and return the map object in Finally, pseudocode as follows:
map<string, object> map = new hashmap<string, object> ();
try{
    if (check ()) {
       map.put ("Message", "Check Error");
	throw new Exception ("Check error");
    }
catch (Exception e) {
   throw e;
} finally{return
   map;
}

The result is that the scenario is not feasible because the exception will not throw when return is finally
2. Passing reference types in ParametersPseudo code is as follows:
<strong>public void Servicemethod (map<string, object> Map) throws exception{
 if (check ()) {
   Map.put ("Message", "Check Error");
   throw new Exception ("Check error");
  }

</strong>

3. Exception classification, return different exceptions for different errors, catch exceptions outside the service method and handleThrow a different exception in the service to indicate a different error state
In the above scenario, scenario 3 is the most consistent interface definition, the parameter is the interface needs, this definition can meet the interface of the service, so the recommended scenario 3



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.