try catch finally

Can someone offer any advive if this is the best way to do a try/catch / finally?

public static double ResponseRate (int surveyId)
        {
            try
            {
                SurveyClass sc = new SurveyClass(surveyId);
                return ResultsClass.GetRespondantsNumber(surveyId) / (sc.PromptsNo * 1.0) * 100;
            }

            catch
            {
                (Exception);
            }

            finally
            {
                ("Executing finally block.");
            }
       
           
        }
bailey1200Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sarkany0Commented:
try
{
   //You try something here, if which is wrong (eg: divide by zero)
}
catch (DivideByZeroException dbze)
{
   //The progrma tried to divide by zero, do something here to handle this exception
}
catch (Exception e)
{
   //DO something to handle this common exception
}
finally
{
   //This part is always running after try, or catch
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lucky_jamesCommented:
yes, the approach appears to be fine. try -catch- finally have to be in this order.
try is set of statements which you want to execute.
catch is the set of statements which we want to excute in case any statement in try throw out exception.
finally is the set of statements which you want to execute at the last, irrespective of the exception being thrown by try block or not. This is the place to do the clean up job.

For sample, check out:
http://msdn.microsoft.com/en-us/library/dszsf989(VS.71).aspx




0
lucky_jamesCommented:
regarding Catch block:
You can put a series of catch blocks. When the exception being thrown, the first catch block which is able to recieve that type of exception will be executed. Only one catch block gets executed. All exceptions are dervied from System.Exception class. So, its always better to keep the derived exception classes earlier in the catch blocks and parents later.

For example, if there are 4 types of exceptions, A, B(derived from A), C(derived from B, D (derived from C)
then always put them as:
catch(D ex)
{}
catch(C ex)
{}
catch(B ex)
{}
catch(A ex)
{}
catch(System.Exception ex)
{}
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

scottlafoyCommented:
remember if you are going to catch an error handle it or throw it. finally is not mandatory to make the try catch work but if used it is a good place to close connections and delete stuff as it runs no matter what the outcome of the try catch.
0
Minh Võ CôngCommented:
You should not call "return" in the try block, Placing it at the end of function/
public static double ResponseRate (int surveyId)
{
      SurveyClass sc = null;
      double dbReturn = 0.0 //default value
      try
            {
                sc = new SurveyClass(surveyId);
                dbReturn =  ResultsClass.GetRespondantsNumber(surveyId) / (sc.PromptsNo * 1.0) * 100;
            }

      catch (DivideByZeroException dbze)
			{
   			//The progrma tried to divide by zero, do something here to handle this exception
   			//
			}
	 catch (Exception e)
			{
   			//DO something to handle this common exception
			}
     finally
           {
                // free resource
                //if (sc!=null) sc.dispose -> example                
            }      
    return dbReturn;      
}

Open in new window

0
MarkusLoiblCommented:
>> You should not call "return" in the try block, Placing it at the end of function
I have to regret. Returning from anywhere in the try block is useful and allowed. The finally block is executed anyway!
Often not returning from within the try block would lead to unreadable code. Returning right where it is needed is much clearer.
0
bailey1200Author Commented:
good advice
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.