---
title: "Encapsulating common exception handling logic"  
description: "Encapsulating common exception handling logic"  
author: "Anonymous User"  
published: 2013-06-12  
updated: 2013-06-12  
canonical: https://www.mindstick.com/forum/1052/encapsulating-common-exception-handling-logic  
category: "oops"  
tags: ["oops"]  
reading_time: 2 minutes  

---

# Encapsulating common exception handling logic

I have a [series](https://answers.mindstick.com/qa/101432/what-s-the-highest-score-in-a-single-world-series-game) of [web service](https://www.mindstick.com/articles/895/web-service-introduction) [method](https://www.mindstick.com/forum/166/webservice-method) calls which all follow the below format. The only [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) in each method is httpRequest.methodName(). Can anybody think of a way I can encapsulate the [common](https://www.mindstick.com/articles/23170/10-most-common-accounting-mistakes-of-small-business) [logic](https://www.mindstick.com/articles/331924/best-ways-to-improve-your-logic-building-skills-for-programming)? Also note that my [environment](https://yourviews.mindstick.com/view/308/need-to-create-a-supportive-work-environment-for-women) is J2ME.

```
public Response webserviceCall(Request request) {     HTTPRequest httpRequest = new HTTPRequest(new ConnectionProperties());     String errorMessage = "";    String errorCode = "";     try {        // the only thing different        httpRequest.methodName();    } catch (SDKException e) {        errorMessage = e.getMessage();        errorCode = e.getErrorCode();    }     Error error = new Error(errorMessage,errorCode);     return new Response(error);}
```

## Replies

### Reply by Vijay Shukla

Hello Ezra Heywood!

One alternative is to put that code in an abstract class, and change it to call an abstract method (name it process, for example):

```
abstract class BaseWebService {
   public abstract Response process(HTTPRequest request) throws SDKException;

   public Response webserviceCall(Request request) {

       HTTPRequest httpRequest = new HTTPRequest(new ConnectionProperties());

       String errorMessage = "";
       String errorCode = "";

       try {
           process(httpRequest);
       } catch (SDKException e) {
           errorMessage = e.getMessage();
           errorCode = e.getErrorCode();
       }

       Error error = new Error(errorMessage,errorCode);

       return new Response(error);
   }
 }
```

Then make each of your services extend that class and implement the process method as needed

\

```
class OneService extends BaseWebService {

   Response process(HTTPRequest request) throws SDKException{
        return request.methodName();
   }
}
```

\

\


---

Original Source: https://www.mindstick.com/forum/1052/encapsulating-common-exception-handling-logic

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
