To work with multiple methods i have to use the same checked exception catch
many times (see Java: checked vs unchecked exception explanation). The result is duplicated lines of code. I would like some way of reducing the duplicated exception catch
to reduce the number of lines of code.
What is a good way to reduce the number of duplicated catch
sections and decrease the lines of code? Is there any way that I can write that catch
operation in one another file and use that explicitly?
Basically i want to reduce line count and make the code more concise and easier to read.
Here is an example of my code:
@RequestMapping(value="")
public @ResponseBody Response addMultiple(){
if() {
try {
data = new weight();
}
catch( AmazonServiceException se ){
response = x;
}
catch (AmazonClientException ce) {
response = y;
}
catch(JsonProcessingException je) {
response = z;
}
}
@RequestMapping(value="")
public @ResponseBody Response addMultiple2(){
if() {
try {
data = new height();
}
catch( AmazonServiceException se ){
response = x;
}
catch (AmazonClientException ce) {
response = y;
}
catch(JsonProcessingException je) {
response = z;
}
}
@RequestMapping(value="")
public @ResponseBody Response addMultiple3(){
if() {
try {
data = new age();
}
catch( AmazonServiceException se ){
response = x;
}
catch (AmazonClientException ce) {
response = y;
}
catch(JsonProcessingException je) {
response = z;
}
}
I want declare that exception catching operation once, and want to call it multiple time.
I am using Spring framework.