I can see few problems. You don't need this exception processing here. Instead, do this:
double refurbRate;
int totalRefurb =
int totalUnits =
System.Diagnostics.Debug.Assert(totalUnits >= 0 && totalRefurb >= 0 && totalUnits >= totalRefurb);
refurbRate = totalUnits > 0 ? (double)totalRefurb * 100 / totalUnits : 0;
Even better:
double refurbRate;
uint totalRefurb =
uint totalUnits =
System.Diagnostics.Debug.Assert(totalUnits >= totalRefurb);
refurbRate = totalUnits > 0 ? (double)totalRefurb * 100 / totalUnits : 0;
Zero percentage for zero total units is quite logical. It should not be the exception.
You should not use integer percentage (if fact, never). In you code, you get wrong coding due to rounding, you could improve it by multiplying by 100 first, but
double
type is better. If you need integer on output, it will be shown via appropriate format in
string.Format
.
You also misuse exceptions: handle it too locally, to early.
Find some instructions of better exception handling 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?[
^]
Good luck,
—SA