I'm coding a webservice in Java using aws and in many method i need to have a try catch block that can actually log any errors that can occur in the execution of each exposed methods.

```
@WebMethod(operationName = "listingBucket")
public String listingBucket() {
    String message = "";        
    try {
        message = "Listing buckets";            
        for (Bucket bucket : s3.listBuckets()) {
            message += " - " + bucket.getName();
        }
    } catch (AmazonServiceException ase) {
        message += "Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.";
        message += "Error Message:    " + ase.getMessage();
        message += "HTTP Status Code: " + ase.getStatusCode();
        message += "AWS Error Code:   " + ase.getErrorCode();
        message += "Error Type:       " + ase.getErrorType();
        message += "Request ID:       " + ase.getRequestId();
    } catch (AmazonClientException ace) {
        message += "Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.";
        message += "Error Message: " + ace.getMessage();
    }
    return message;
}
@WebMethod(operationName = "addObjectToBucket")
public String addObjectToBucket(String bucketName, String objectName, File file) throws IOException{
    if ( file == null ){
        file = createSampleFile();
    }
    String message = "";        
    try {
        message += "Uploading a new object to S3 from a file\n";     
        s3.putObject(new PutObjectRequest(bucketName, objectName, file));
    } catch (AmazonServiceException ase) {
        message += "Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.";
        message += "Error Message:    " + ase.getMessage();
        message += "HTTP Status Code: " + ase.getStatusCode();
        message += "AWS Error Code:   " + ase.getErrorCode();
        message += "Error Type:       " + ase.getErrorType();
        message += "Request ID:       " + ase.getRequestId();
    } catch (AmazonClientException ace) {
        message += "Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.";
        message += "Error Message: " + ace.getMessage();
    }
    return message;        
}

```

How Can i avoid to repeat this try catch block throw all methods that use this kind of stuff ? 

Thanks for your help !

Edit : Actually I modified the code : 

```
private String parseError(AmazonServiceException ase) {
    String message;
    message = "Caught an AmazonServiceException, which means your request made it "
            + "to Amazon S3, but was rejected with an error response for some reason.";
    message += "Error Message:    " + ase.getMessage();
    message += "HTTP Status Code: " + ase.getStatusCode();
    message += "AWS Error Code:   " + ase.getErrorCode();
    message += "Error Type:       " + ase.getErrorType();
    message += "Request ID:       " + ase.getRequestId();
    return message;
}

private String parseError(AmazonClientException ace) {
    String message;
    message += "Caught an AmazonClientException, which means the client encountered "
            + "a serious internal problem while trying to communicate with S3, "
            + "such as not being able to access the network.";
    message += "Error Message: " + ace.getMessage();
    return message;
}

@WebMethod(operationName = "listingBucket")
public String listingBucket() {
    String message = "";
    try {
        message = "Listing buckets";
        for (Bucket bucket : s3.listBuckets()) {
            message += " - " + bucket.getName();
        }
    } catch (AmazonServiceException exc) {
        message += parseError(exc);
    } catch (AmazonClientException exc) {
        message += parseError(exc);
    }
    return message;
}

```

Clearer indeed ! :)

I'll just take a look about the command pattern to see if I can use it for this kind of application.