Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to handle divide by zero exception using “DivideByZeroException” class in VB and C# (.net).

Author: Shehzad Hemani
Download Source Code : 929_code.zip

In this simple article you will learn how we can handle divide by zero exception using “DivideByZeroException” class in VB and C# (.net).

 DivideByZeroException:

This class is used to handle the exception which occurs when a number is divided by a zero. To handle this exception .net provides us a class named DivideByZeroException.

DivideByZeroException Handling:

We can handle divide by zero exception by writing try catch statement and passing DivideByZeroException object.

To demonstrate make a window application. Drag one button and two text boxes on form. Enter values in text boxes and press button.

Now write the following code on Button click event:

C#

private void btn_divide_Click(object sender, EventArgs e)

        {

            int a =Convert.ToInt16(textBox1.Text);

            int b = Convert.ToInt16(textBox2.Text);

            try

            {

                int sum=a/b;

                MessageBox.Show(sum.ToString());

            }

            catch (DivideByZeroException ex) {

                MessageBox.Show("Exception Occured Do Not Divide By Zero");

                }

        }

VB

Private Sub btn_divide_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim a As Integer = Convert.ToInt16(textBox1.Text)

        Dim b As Integer = Convert.ToInt16(textBox2.Text)

        Try

            Dim sum As Integer = a / b

            MessageBox.Show(sum.ToString())

        Catch ex As DivideByZeroException

            MessageBox.Show("Exception Occured Do Not Divide By Zero")

        End Try

    End Sub

This simple code catches the divide by zero exception.

 

 

Message:

To show an appropriate message to user we can write a message in catch block as mentioned in above code. But there is an other way to show an appropriate message. DivideByZeroException class provides us a property named message. This property returns us a string. The string contains the appropriate message which is already defined by the class.

To demonstrate make a window application. Drag one button and two text boxes on form. Enter values in text boxes and press button.

Now write the following code on Button click event:

C#

private void btn_divide_Click(object sender, EventArgs e)

        {

            int a =Convert.ToInt16(textBox1.Text);

            int b = Convert.ToInt16(textBox2.Text);

            try

            {

                int sum=a/b;

                MessageBox.Show(sum.ToString());

            }

            catch (DivideByZeroException ex) {

                MessageBox.Show(ex.Message);

                }

        }

VB

Private Sub btn_divide_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim a As Integer = Convert.ToInt16(textBox1.Text)

        Dim b As Integer = Convert.ToInt16(textBox2.Text)

        Try

            Dim sum As Integer = a / b

            MessageBox.Show(sum.ToString())

        Catch ex As DivideByZeroException

            MessageBox.Show(ex.Message)

        End Try

    End Sub

This simple code catches the divide by zero exception and displays a message provided by class.

Source:

If you want to know that in which application exception occurred for this purpose .net provides us a property named source. This property takes and returns a string. This string contains the name of application.

To demonstrate make a window application. Drag one button and two text boxes on form. Enter values in text boxes and press button.

Now write the following code on Button click event:

C#

private void btn_divide_Click(object sender, EventArgs e)

        {

            int a =Convert.ToInt16(textBox1.Text);

            int b = Convert.ToInt16(textBox2.Text);

            try

            {

                int sum=a/b;

                MessageBox.Show(sum.ToString());

            }

            catch (DivideByZeroException ex) {

                string src = ex.Source;

                MessageBox.Show(src);                }

        }

VB

Private Sub btn_divide_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim a As Integer = Convert.ToInt16(textBox1.Text)

        Dim b As Integer = Convert.ToInt16(textBox2.Text)

        Try

            Dim sum As Integer = a / b

            MessageBox.Show(sum.ToString())

        Catch ex As DivideByZeroException

            Dim src As String = ex.Source

            MessageBox.Show(src)

        End Try

    End Sub

This simple code catches the divide by zero exception and displays application name in which exception occurred.

This was the application name which is given by the class automatically. If you want to assign the name of your own choice then you can set the name.

To demonstrate make a window application. Drag one button and two text boxes on form. Enter values in text boxes and press button.

Now write the following code on Button click event:

C#

private void btn_divide_Click(object sender, EventArgs e)

        {

            int a =Convert.ToInt16(textBox1.Text);

            int b = Convert.ToInt16(textBox2.Text);

            try

            {

                int sum=a/b;

                MessageBox.Show(sum.ToString());

            }

            catch (DivideByZeroException ex) {

               ex.Source="My Devasp.net Application";

                MessageBox.Show(ex.Source);

               }

        }

VB

Private Sub btn_divide_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim a As Integer = Convert.ToInt16(textBox1.Text)

        Dim b As Integer = Convert.ToInt16(textBox2.Text)

        Try

            Dim sum As Integer = a / b

            MessageBox.Show(sum.ToString())

        Catch ex As DivideByZeroException

ex.Source = "My Devasp.net Application"

            MessageBox.Show(ex.Source)

        End Try

    End Sub

Stack Trace:

If you want to get the stack trace of that time when exception occurred for that purpose we have a property named stack trace. This property returns a string.

To demonstrate make a window application. Drag one button and two text boxes on form. Enter values in text boxes and press button.

Now write the following code on Button click event:

C#

private void btn_divide_Click(object sender, EventArgs e)

        {

            int a =Convert.ToInt16(textBox1.Text);

            int b = Convert.ToInt16(textBox2.Text);

            try

            {

                int sum=a/b;

                MessageBox.Show(sum.ToString());

            }

            catch (DivideByZeroException ex) {

                MessageBox.Show(ex.StackTrace);                }

        }

VB

Private Sub btn_divide_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim a As Integer = Convert.ToInt16(textBox1.Text)

        Dim b As Integer = Convert.ToInt16(textBox2.Text)

        Try

            Dim sum As Integer = a / b

            MessageBox.Show(sum.ToString())

        Catch ex As DivideByZeroException

            MessageBox.Show(ex.StackTrace)

        End Try

    End Sub

This simple code gets the stack trace.

Hyper Link:

If you want to get or set the hyper link help full for handling the exception for this purpose we have a property named hyper link. This property takes and returns a string. You can set the link and at the time of exception can show this to the user.

To demonstrate make a window application. Drag one button and two text boxes on form. Enter values in text boxes and press button.

Now write the following code on Button click event:

C#

private void btn_divide_Click(object sender, EventArgs e)

        {

            int a =Convert.ToInt16(textBox1.Text);

            int b = Convert.ToInt16(textBox2.Text);

            try

            {

                int sum=a/b;

                MessageBox.Show(sum.ToString());

            }

            catch (DivideByZeroException ex) {

                ex.HelpLink = "www.devasp.net";

                MessageBox.Show(ex.HelpLink);

                }

        }

VB

Private Sub btn_divide_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim a As Integer = Convert.ToInt16(textBox1.Text)

        Dim b As Integer = Convert.ToInt16(textBox2.Text)

        Try

            Dim sum As Integer = a / b

            MessageBox.Show(sum.ToString())

        Catch ex As DivideByZeroException

            ex.HelpLink = "www.devasp.net"

            MessageBox.Show(ex.HelpLink)

        End Try

    End Sub

This simple code set and gets the hyper link.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = " Devasp Exception Handling Application";          

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = " Devasp Exception Handling Application"

    End Sub

 

This simple article tells that how we can handle divide by zero exception using “DivideByZeroException” class in VB and C# (.net).

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Comparison of two objects using “CaseInsensitiveComparer” class in VB and C# (.net).

Disclaimer - Privacy
© 2002-2017 DevASP.net