Strange behaviour of Try Catch

Below is a simple console routine that should (I think) catch a DivideByZeroException - it does not do so and I cannot figure out why? Using VS2008

Module Module1

    Sub Main()

        Dim x As Int16 = 12
        Dim y As Int16 = 0
        Dim z As Int16

            z = x / y
        Catch ex1 As DivideByZeroException
            Console.WriteLine("Error: Attempt to Divide by Zero")
            'Catch ex2 As Exception
            '    Console.WriteLine("Error " & ex2.Message)
        End Try


    End Sub

End Module
Who is Participating?
Deepak LakkadCommented:

In case of Int16 datatype, it throws OverflowException Exception, while if you user decimal datatype your code will throw DivideByZeroException

- Deepak Lakkad
Deepak LakkadCommented:

This will happen becuase of when you divide an integer with 0 it will produce NaN.

Deepak LakkadCommented:

You can use \ [integer division operator] in place of / to produce DivideByZeroException

- Deepak Lakkad
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.