user3409650
user3409650

Reputation: 523

How to return the value of the method in java inside another method

​what I want to ask may seem stupid but since I am beginner I dare to ask. Actually I have a method that returns a string DataReader.getInstance().getData(); and I want to replace the content of below string which is "data is assigned" by the value that this method returns.

response.put("data is assigned", + result.get());

Is there any nice way of doing that instead of just simply replacing. The problem is that when I replace like this:

String value = DataReader.getInstance().getData();
response.put(value + result.get());

it complains that it needs a try, catch and when I do that

try {
    String value =  DataReader.getInstance().getData();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

then Value is just defined inside the trycatch! but anyway I need a clever way other than that. Any idea.

Thanks,

Upvotes: 1

Views: 327

Answers (4)

Michał Schielmann
Michał Schielmann

Reputation: 1382

There are a few solutions:

1st:

try {
    response.put(DataReader.getInstance().getData() + result.get());
} catch (IOException e) {
    e.printStackTrace();
}

2nd

try {
    String value = DataReader.getInstance().getData();
    response.put(value + result.get());
} catch (IOException e) {
    e.printStackTrace();
}

3rd:

String value = null;
try {
    value = DataReader.getInstance().getData();
    response.put(value + result.get());
} catch (IOException e) {
    e.printStackTrace();
}

4th:

String value = null;
try {
    value = DataReader.getInstance().getData();
} catch (IOException e) {
    e.printStackTrace();
}
response.put(value + result.get());

5th

Write a method to get data:

private String getReaderData()
{
    try {
        return DataReader.getInstance().getData();
    } catch (IOException e) {
        //handle exception - throw the same, throw wrapper, return null
        throw new IllegalStateException("No Data Found in the reader");
    }
}

And then use it like this:

response.put(getReaderData() + result.get());

Upvotes: 1

Nathan Hughes
Nathan Hughes

Reputation: 96454

The purpose of catching an exception is to do something with it: log it, use it to decide what to do next, whatever. Usually for IOExceptions you're just done, all you can do is log it.

The reason for throwing exceptions is because usually the place where an exception is thrown is not the place where it can be handled meaningfully, throwing the exception lets the program leave its current context, which the exception has demonstrated to be invalid, and go to a different place, where the program can catch the exception and respond appropriately.

Here the exception is telling you what you are trying to do can't be done, so there's really no point in continuing. The exception gives you away to abort and regroup.

You could change your example to:

try {
    return DataReader.getInstance().getData();
} catch (IOException e) {
    e.printStackTrace();
}
return null;

But then the callers have to add code to check for the case where you return null. You're better off letting the exception be thrown (aborting the current operation), then letting exceptions be handled in a central spot that logs them. Then your method is just

String myMethod() throws IOException {
    return DataReader.getInstance().getData();
}

Upvotes: 1

Andremoniy
Andremoniy

Reputation: 34920

In addition to present asnwers one more way.

Sometimes (e.g. in more complex situations) it is convenient to define new method, that wraps this try...catch code:

response.put(buildVale() + result.get());
...
private static String buildValue() {
    try {
         return DataReader.getInstance().getData();
    } catch (IOException e) {
         e.printStackTrace();
         throw new RuntimeException(e);
    }        
}

Upvotes: 0

Eran
Eran

Reputation: 394146

Define value outside the try :

String value = null;
try {
    value =  DataReader.getInstance().getData();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Upvotes: 4

Related Questions