Re: finally block does not complete normally - Eclipse



On Tue, 31 May 2005 12:42:52 -0700, dannyDog wrote:

> I've started working on a new project using Eclipse. When I do a build
> I get multiple warnings stating "finally block does not complete
> normally".
>
> A sample method follows:
> /**
> * Property getter for destination type
> * @return type
> */
> public String getDestinationType()
> {
> String destType = "SCHOOL_DISTRICT";
> try
> {
> destType = this.getReceiving().getDestinationType();
> if (destType.length() == 0)
> {
> destType = "SCHOOL_DISTRICT";
> }
> }
> catch (Exception e)
> {
> destType = "SCHOOL_DISTRICT";
> }
> finally
> {
> return destType;
> }
> }
>
> This type of coding of returning the return value in the finally block
> is in many, many, many methods over many, many, many classes.
>
> What is the correct way to code try/catch/finally return value?

break, continue return and throw are abrupt completions of statements.
You shoud do the return outside the "finally" block.
Something like this:
/**
* Property getter for destination type
* @return type
*/
public String getDestinationType()
{
String destType = "SCHOOL_DISTRICT";
try
{
destType = this.getReceiving().getDestinationType();
if (destType.length() == 0)
{
destType = "SCHOOL_DISTRICT";
}
}
catch (Exception e)
{
destType = "SCHOOL_DISTRICT";
}
finally
{
//some code that NEEDS to be executed no matter what, such as
//closing a connection to a database or other stuff that
//should happen always.
//otherwise don't use finally block.
}
return destType;
}

Anyway, I would write it like this:

/**
* Property getter for destination type
* @return type
*/
public String getDestinationType() {
String destType = null;
try {
destType = this.getReceiving().getDestinationType();
}catch (Exception e){}
if(destType == null || destType.length() == 0) {
destType = "SCHOOL_DISTRICT";
}
return destType;
}


.