Best way of handling exception for java non void method
Hi.........
i am trying find out the best way of handling exception for the below method
public String changeString(String source) throws SomeException{
String target;
.....i am modifying source string here and assigning to target
.
.............
.........
return target;
}
now calling method gets the String as return type even if in exception occurs.
what am trying to get here is if exception occurs method should not return target ,it should return exception
Re: Best way of handling exception for java non void method
Huh? What happens instead when an Exception is thrown? It would be easier to help you if you provided an SSCCE that demonstrated the problem.
Re: Best way of handling exception for java non void method
Quote:
if exception occurs method should not return target ,it should return exception
Problem with your terminology or concepts
The method
either RETURNS a String
or it can THROW an exception.
The way the code is written, if there is no exception, it will return target.
If it throws the exception, target will not be returned. The enclosing catch clause will be executed.
Can you explain what you want to do different from what the code seems to say.