Divide by Zero exception

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there

My .asp application draws data from a number of sources, converts the
results to type.double and does various calculations with these figures.
Because of data entry errors on the system from which some of the data is
drawn (this system is unfortunately not under my control so I can not sort
this problem out at source....) I occasionally get Divide by Zero Exceptions
which crash my application.

Does anyone have a clever (or otherwise) way of catching this particular
error and inserting a value of 0 in to the result instead of crashing ?

Any help would be appreciated
 
When you are doing your calculations, check if you are dividing by something
that is zero, and handle apropriately. The lazier way to do this would be to
put a Try/catch around the calculations and specificly handle
DivdeByZeroException and put a zero in instead in the catch block.
 
I am assuming since you posted this in an ADONET NG< your data resides in a
datatable?

Bad but obvious solution -
Put a try catch block - catch DivideByZeroException.
Better but not so obvious solution -
Use the RowAdded event on the datatable to "clean" the data as it is being
filled - so if you see a null, put a zero in there.

- Sahil Malik [MVP]
http://codebetter.com/blogs/sahil.malik/
 
Back
Top