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.
A general way to handle it could be to create a method that can handle all your exception types and return a Response
object based on the passed in Exception
.
The handleException
method:
public static Response handleException (Exception e)
{
Response res = null;
if (e instanceof AmazonServiceException)
{
res = x;
}
else if (e instanceof AmazonClientException)
{
res = y;
}
else if (e instanceof JsonProcessingException)
{
res = z;
}
else // if we get any other exception
{
res = xyz;
// OR you can throw new RuntimeException(e);
}
return res;
}
Now, all the calling code where you don't want to repeat all the exception handling would use it like this:
@RequestMapping(value="")
public @ResponseBody Response addMultiple(){
if() {
try {
data = new weight();
}
// Use this same catch block in the other addMultiple() methods
catch(Exception e)
{
response = handleException(e);
}
}
The answer of @UUID will allow you to handle exceptions outside the context of the function. If you want to handle them inside the function, you can write a handleException
method, that will allow you return values and have parameters:
public class ExceptionHandler {
public static Response handleWebException(Object ... parameters) {
return ... some result ...;
}
}
In your method you call
public @ResponseBody Response addMultiple2(){
if() {
try {
data = new height();
}
catch( Exception ex ){
return ExceptionHandler.handleWebException(ex);
}
}
}
You'd write a custom exception handler in Spring if you want to handle these exception and take some action, the handler will take if there any exception occurs, also you can omit the try-catch
blocks.
@ExceptionHandler({ Exception.class }) //narrow it to the exception you need
public ModelAndView globalExceptionHandler(Exception e) {
return new ModelAndView("error");
}
Otherwise catch Exception
in your methods and write a handler method to handle it in catch block.
change catch block with
catch( Exception e ){
handleException(e);
}
and handler below
private Response handleException(Exception e){
if(e instanceof E1){
}else if(e instanceof E2){
}
.
.
.
return response;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With