Free Information Technology Magazines and eBooks

Thursday, April 16, 2009

How to Catch Divide By Zero Exception in .NET

Today's tutorial for beginners is about the most common runtime error on programs involving mathematical formulas, the Divide By Zero Exception.This DivideByZeroException runtime error can close your software unexpectedly, leaving your end-users hanging on unsaved works. In .NET, we can prevent error like DivideByZeroException by trapping it with try catch control structure. Take a look at the example codes below:

C#

private void button1_Click(object sender, EventArgs e)
{
int Val1 = 0;
int Val2 = 57;
int Result = 0;

try
{
// No error
Result = DivideNum(Val1 , Val2);
MessageBox.Show(Result.ToString());

//With error, this should throw the dividebyzeroexception
Result = DivideNum(Val2, Val1);
MessageBox.Show(Result.ToString());

}
catch (DivideByZeroException ex)
{
MessageBox.Show(ex.Message);
}
}

static public int DivideNum(int num, int denom)
{
return (num / denom);
}



In VB.NET, its a little bit tricky. If we convert the C# code to VB.NET our code should look like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Val1 As Integer = 0
Dim Val2 As Integer = 57
Dim Result As Integer = 0

Try
'No error
Result = DivideNum(Val1, Val2)
MessageBox.Show(Result.ToString())

'With error, this should throw the dividebyzeroexception
Result = DivideNum(Val2, Val1)
MessageBox.Show(Result.ToString())

Catch ex As DivideByZeroException
MessageBox.Show(ex.Message)
End Try

End Sub
Public Function DivideNum(ByVal num As Integer, ByVal denom As Integer) As Integer
Return (num / denom)
End Function


But the problem is the System.DivideByZeroException does not catch the exception and display a different error as shown below.

System.OverflowException

If we use the System.OverflowException it will work. The reason behind this is that the "/" operator performs floating point division, which doesn't throw a DivideByZeroException but instead it returns NaN or Infinity. If you add Option Strict On on your program, the IDE will underline the error like the one below:

DivideByZeroExeception runtime error


To solve it we should replace "/" with "\". The working code for VB.NET will be:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Dim Val1 As Integer = 0
Dim Val2 As Integer = 57
Dim Result As Integer = 0

Try
'No error
Result = DivideNum(Val1, Val2)
MessageBox.Show(Result.ToString())

'With error, this should throw the dividebyzeroexception
Result = DivideNum(Val2, Val1)
MessageBox.Show(Result.ToString())

Catch ex As DivideByZeroException
MessageBox.Show(ex.Message)
End Try

End Sub
Public Function DivideNum(ByVal num As Integer, ByVal denom As Integer) As Integer
Return (num \ denom)
End Function


Now using this "try catch" statement will make your programs DivideByZeroException runtime error free. Good Luck!

0 comments: