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?
 
Sarkany0Connect With a Mentor Commented:
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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ôngConnect With a Mentor Commented:
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
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.