return a value from Try - Catch block?

Hello experts,

I am trying to secure my code by using try catch blocks where necessary. Here is a method I use:

 public int doesExist()
        {
                conn.ConnectionString = strConn;
                conn.Open();

                cmdCheck.Connection = conn;
                cmdCheck.CommandText = ConfigurationSettings.AppSettings["spCheck"];
                cmdCheck.CommandType = CommandType.StoredProcedure;
                int iq = (int)cmdCheck.ExecuteScalar();    

               return iq;
        }

Whenever I want to put this piece between  a try block, I get this error:

Error      5      The name 'iq' does not exist in the current context

How do I secure my code now?

Thanks
bozerAsked:
Who is Participating?
 
mrichmonCommented:
You don't put try-catches around function declarations. (and also try/catch is not a way to secure code...)

You can put around the call

int result = -1;

try
{
   result = doesExist();
}
catch(whatever exception you are catching)
{
   // do stuff
}

//use result...
0
 
bozerAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.