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