throwing an exception in c#

Hi,
Consider this function:

private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
            {
                  if ((commandParameters == null) || (parameterValues == null))
                  {
                        //do nothing if we get no data
                        return;
                  }

                  // we must have the same number of values as we pave parameters to put them in
                  if (commandParameters.Length != parameterValues.Length)
                  {
                        throw new ArgumentException("Parameter count does not match Parameter Value count.");
                  }

                  //iterate through the SqlParameters, assigning the values from the corresponding position in the
                  //value array
                  for (int i = 0, j = commandParameters.Length; i < j; i++)
                  {
                        commandParameters[i].Value = parameterValues[i];
                  }
            }

In this function the author is throwing an error by using:

throw new ArgumentException("Parameter count does not match Parameter Value count.");
But he is not using any try-catch block here nor any part of the part of the program which calling this function. So how this will get handles if any error occured and what is the point of throwing an exception without handlig with a try-catch block?

Thanks in advance!

ube100
ube100Asked:
Who is Participating?
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.

DhaestCommented:
Creating and Throwing Exceptions (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/ms173163.aspx

Things to Avoid When Throwing Exceptions
The following list identifies practices to avoid when throwing exceptions:

Exceptions should not be used to change the flow of a program as part of ordinary execution. Exceptions should only be used to report and handle error conditions.

Exceptions should not be returned as a return value or parameter instead of being thrown.

Do not throw System..::.Exception, System..::.SystemException, System..::.NullReferenceException, or System..::.IndexOutOfRangeException intentionally from your own source code.

Do not create exceptions that can be thrown in debug mode but not release mode. To identify run-time errors during the development phase, use Debug Assert instead.
0
HarryNSCommented:
If you analyze the function you have pasted, following are the two possibility of exceptions
1) When any one of the parameters are NULL, which is handled in the first IF condition.
2) As values are copied from one array to another assuming both will contain same no. of items, commandParameters[i].Value = parameterValues[i];, If one array is shorter than the other, there is a possibility of exception.

Apart from above two scenarios, there is no other possibility of exceptions will be thrown.

Programmer tried to catch the exception within the code itself. It is one good way of programming. When you are certain at some places where exceptions might occur, you handle it yourself.

0
ube100Author Commented:
HarryNS

O.K, I understands that but if an error occured and how will the user see this error message If we don't use the:

try
{
}
catch (ArgumentException argex)
{
     Cosole.Writeline argex.Message;
}
As I heve seen in this project from the code where he is calling this function NOT using any try-catch block to catch the error. The example I have pasted below:

public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
            {
                  //if we receive parameter values, we need to figure out where they go
                  if ((parameterValues != null) && (parameterValues.Length > 0))
                  {
                        //pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
                        SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

                        //assign the provided values to these parameters based on parameter order
                        AssignParameterValues(commandParameters, parameterValues);

                        //call the overload that takes an array of SqlParameters
                        return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
                  }
                        //otherwise we can just call the SP without params
                  else
                  {
                        return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
                  }
            }
As you can see here he is calling the AssignParameterValues function but there isn't ant try-catch block.
0
HarryNSCommented:
Throw new exception will throw an exception back to the parent function.  

For Ex:

try
{
AssignParameterValues(commandParameters, parameterValues)

}
catch (ArgumentException argex)
{
     Cosole.Writeline argex.Message;
}
0
DhaestCommented:
By the way, if you throw an exception, it will stack up until it reaches the highest level of your code. If not handled there, the current thread (where the error occurs) will be terminated (and mostly the program stops working than)
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
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
.NET Programming

From novice to tech pro — start learning today.