Try Catch and Variables

  • Thread starter Thread starter Bungle
  • Start date Start date
B

Bungle

Hi

I am trying to do something really simple. Create a method within a
class which will connect to the database, pull back a result and
return it out the method. Not hard.

All my database connections and executescalar work fine, but, I want
to put it into a Try Catch statement so that if a problem occurs, it
will error out to my error page.

How can I do all this and return a value within the Try Catch???

public int getUserIdByUsernamePassword(String username, String
userPassword)
{
try
{
databaseConnect.Open();

int userId;

cmdGetUserDetailsByUsernamePassword.Parameters["@username"].Value =
username;
cmdGetUserDetailsByUsernamePassword.Parameters["@userPassword"].Value
= userPassword;

userId = (int)cmdGetUserDetailsByUsernamePassword.ExecuteScalar();

return userId

}
catch
{
// Error page call.
}

}

The method does not recognise the fact that I have a returned a value.

Hopefully someone can give me an explanation how I can still have the
benefits of a Try Catch statement while still being able to return a
value out of the method.

Thank you very much
 
Bungle said:
public int getUserIdByUsernamePassword(String username, String
userPassword)
{ int userId -1;
try
{
databaseConnect.Open();



cmdGetUserDetailsByUsernamePassword.Parameters["@username"].Value =
username;
cmdGetUserDetailsByUsernamePassword.Parameters["@userPassword"].Value
= userPassword;

userId = (int)cmdGetUserDetailsByUsernamePassword.ExecuteScalar();

} catch(Exception ex)
{
// Error page call.
}
finally
{
return userId
}

This for example should solve all your problems, if I get your problem
right.

The finally statement is executed every time after the catch was
processed, or the try worked perfectly.

I use this for Database rollback or commit stuff. A very usefull construct!

Best Regards,

Martin
 
Hi,

Put a return statement into catch block or at the end of method.
You'll make compiler happy.
 
Thanks for the reply Martin.

Unfortunately it does not like the return in the finally statement.

It gives the error "Control cannot leave the body of a finally clause"??

These were the kind of walls I kept hitting.

Thanks
 
Hi,

There is no need for a return statement in finally block as if (unhandled)
exception happens there is no return value anyway.

--
Miha Markic [MVP C#] - DXSquad/RightHand .NET consulting & software
development
miha at rthand com www.rthand.com

Developer Express newsgroups are for peer-to-peer support.
For direct support from Developer Express, write to (e-mail address removed)
Bug reports should be directed to: (e-mail address removed)
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.

mphanke said:
Bungle said:
public int getUserIdByUsernamePassword(String username, String
userPassword)
{ int userId -1;
try
{
databaseConnect.Open();



cmdGetUserDetailsByUsernamePassword.Parameters["@username"].Value =
username;
cmdGetUserDetailsByUsernamePassword.Parameters["@userPassword"].Value
= userPassword;

userId = (int)cmdGetUserDetailsByUsernamePassword.ExecuteScalar();

} catch(Exception ex)
{
// Error page call.
}
finally
{
return userId
}

This for example should solve all your problems, if I get your problem
right.

The finally statement is executed every time after the catch was
processed, or the try worked perfectly.

I use this for Database rollback or commit stuff. A very usefull construct!

Best Regards,

Martin
 
You need to either return a value (say -1) in you catch block, move
the return to after the catch block, or rethrow the exception in you
catch block.

All paths through the function must either return an appropriate value
or throw an exception.

Adam
 
There is no need for a return statement in finally block as if (unhandled)
exception happens there is no return value anyway.

Well, that depends on what the language specifies. Java specifies that
if a finally clause completes abruptly (eg a return or an exception)
then the result of the whole method is that abrupt completion - in
other words, a return in a finally clause can sort of "override" any
exception (or other return value).

C# just doesn't allow return from finally at all, so no semantics need
to be defined for that bit.
 
Hi Jon,

Jon Skeet said:
Well, that depends on what the language specifies. Java specifies that
if a finally clause completes abruptly (eg a return or an exception)
then the result of the whole method is that abrupt completion - in
other words, a return in a finally clause can sort of "override" any
exception (or other return value).

C# just doesn't allow return from finally at all, so no semantics need
to be defined for that bit.

I was kinda talking about C# :-)
 
I was kinda talking about C# :-)

Sure, so was I. Your statement about "there's no need for a return
value" assumes that if you *could* return from a finally block, the
return value would be ignored. I was pointing out that this assumption
is invalid as the language designers could choose to do whatever they
want - just as the Java designers chose that the return value would
take precedence over the exception.
 
Miha said:
Hi,

There is no need for a return statement in finally block as if (unhandled)
exception happens there is no return value anyway.
Okay,

I mixed up stuff - didn't have the source with me...
This is one of the things I just can't keep in mind!

Martin
 
Let's address this one step at a time.

The first problem is that compile is say that no all paths return a
value, because, it's assuming that the catch is dealing with the problem and
the function is continuing on after the catch.

Solution: Move the return outside the try/catch.
public int getUserIdByUsernamePassword(String username, String userPassword)
{
try
{
// :
}
catch
{
// :
}
return userId
}

New Problem: Undefined variable error, as userId is now out of scope.
(it lived & died inside the try{})

Solution: Move the definition of userId outside of the try/catch.

public int getUserIdByUsernamePassword(String username, String userPassword)
{
int userId;

try
{
// :
}
catch
{
// :
}
return userId
}

Newer problem: The compiler should now complain about the possibility of
return an uninitialized variable.

Solution: Initialize userId to an error value when it is defined:

public int getUserIdByUsernamePassword(String username, String userPassword)
{
int userId = 0;

try
{
// :
}
catch
{
// :
}
return userId
}


--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Back
Top