This
try
{
throw new Exception(ErrorConstants.selectTheState);
}
catch (Exception exception)
{
throw exception;
}
is equivalent to this:
try
{
throw new Exception(ErrorConstants.selectTheState);
}
catch {
throw;
}
and to this:
throw new Exception(ErrorConstants.selectTheState);
That's it, makes no sense. You did not catch the exception. Come to think about, you should not.
You probably mean to put under the
try
block this:
if (StateName1.Text == string.Empty || StateName1.Text == StringConstants.select ||
StateName1.SelectedValue.ToString() == StringConstants.select)
Here,
StateName1.SelectedValue
could be
null
, StateName1 should not be null (and most likely it is not). So, you should not assume something is selected, it throws exception at the attempt to call
ToString
.
Consider:
if (StateName1.Text == string.Empty ||
StateName1.Text == StringConstants.select ||
((StateName1.SelectedValue != null) && (StateName1.SelectedValue.ToString() == StringConstants.select)))
Please see my directions on exception practices here:
How do i make a loop that will stop when a scrollbar reaches the bottom[
^],
When i run an application an exception is caught how to handle this?[
^].
—SA