Not all code paths return a value [Resolved]

Posted by Gh1234567 under Regular Expressions on 4/19/2011 | Points: 10 | Views : 2688 | Status : [Member] | Replies : 5
public bool CloseConn()
{
try
{
if (Oraclecon.State == System.Data.ConnectionState.Open | Oraclecon.State == null)
{
Oraclecon.Close();
return true;
}
}
catch (Exception ex)
{
return false;
}

}




Responses

Posted by: Gh1234567 on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down

Resolved
I solved it ... to be used as below
Boolean isclosed=false;


Gh1234567, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

You get that error because in the Try block when the if condition fails then it does not return anything , but the function expects a return value.

Either set the else part with a return boolean or mention a default return at the end of function.

Thanks,
Debata

Gh1234567, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sashys on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down
The way the compiler recognizes the return value based on signature is out side of Try catch block which run always. There is a possibility that try or catch may not hit all the time. So please use Return Statement at last ie out side the
try catch always...

public bool CloseConn()

{
boolean isclosed=false;
try
{
if (Oraclecon.State == System.Data.ConnectionState.Open | Oraclecon.State == null)
{
Oraclecon.Close();
isclosed=true;
}
}
catch (Exception ex)
{
isclosed=false;
}
return isclosed;
}


Sasikumar .M

Gh1234567, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gh1234567 on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down
The type or namespace name 'boolean' could not be found (are you missing a using directive or an assembly reference?)

Gh1234567, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down
Dear Sir,

I your method after catch block

Please write
return false;

Thank You
Debata

Gh1234567, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response