Try / Catch

  • Thread starter Thread starter jez
  • Start date Start date
J

jez

Two quick questions :

I'm try to using as many as possibly try/catch clauses however :

1) for many methods (such as DataSet.GetXml) there is no specific
exception. Would using the main Exception be giving enough error
information to the user ?

2) for some of my own-made methods I get the following error when
compiling : "not all code paths return a value". This seems to only
happen when I have an if/else without a return in it (even though
there's another if/else in the method that DOES return a value). What
can I do about it ?

jez
 
Inline....
1) for many methods (such as DataSet.GetXml) there is no specific
exception. Would using the main Exception be giving enough error
information to the user ?

Your exception handlers should be doing just that, handling it, not simply
throwing it as a messagebox to the user. If a message needs to be
displayed, you as the code author are in the best position to give something
meaningful to the user about why something isn't working. You should never
give them something like "MissingMethodException Occurred", but instead
something more friendly like "MyLibrary.dll cannot be located. Make sure
you have installed MyReferenceApp. See www.mysite.com for more
information".
2) for some of my own-made methods I get the following error when
compiling : "not all code paths return a value". This seems to only
happen when I have an if/else without a return in it (even though
there's another if/else in the method that DOES return a value). What
can I do about it ?

It means there exists some code path that will not return a value. The fix
is simply to have a return for all cases. If you post the code, we'll point
out where the problem is.
 
Hi Jez,
I'm try to using as many as possibly try/catch clauses however :

Use them smart, an exception is a very costly operation ! , do not use a
try/catch on each method, use them when/where you need them .
1) for many methods (such as DataSet.GetXml) there is no specific
exception. Would using the main Exception be giving enough error
information to the user ?

That depend of what you want the user to see, usually you catch the
exception and show an adecuate message to the user, meaningful on the
context where the application failed, It's not the same if the user sees
"NullReferenceException" that "An error occur in the operation ..... "
Beside, the messages in the CF are more than cryptic , even for developers
:)
2) for some of my own-made methods I get the following error when
compiling : "not all code paths return a value". This seems to only
happen when I have an if/else without a return in it (even though
there's another if/else in the method that DOES return a value). What
can I do about it ?

There is some combination of if and elses that does not ends with a return,
solution:
1- Check your code
2- out a default return at the end.

Cheers,
 
I do always try to give a meaningful explanation for the error - it's
only a pitty that there's not a specific exception for all the methods
in the api. Would one not want to use try/catch as much as possible ?

Example of a method that does always return a value just not with all
if/else clauses (this method gives an error when I try to use a
try/catch with it) :

private Boolean compareDate() {

string year = null;
System.DateTime dateToday =
new System.DateTime(System.DateTime.Now.Year,
System.DateTime.Now.Month, System.DateTime.Now.Day);

if (nudYear.Text.Length ==1){
year = "200"+nudYear.Text.ToString();
}

else {
year = "20"+nudYear.Text.ToString();
}

System.DateTime valueFromNDD =
new System.DateTime(Convert.ToInt32(year),
Convert.ToInt32(nudMonth.Text), Convert.ToInt32(nudDay.Text));

int comparedValue = dateToday.CompareTo(valueFromNDD);

if (comparedValue<0) {
return true;
}

else{
return false;
}
}

Do you see where my problem is ? The first if/else clause makes a
change to a variable and the second if/else clause returns a value.
Whatever happens, a value will always be returned.
 
Back
Top