Strange behaviour of Try Catch

Below is a simple VB.net 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

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

        Console.ReadLine()

    End Sub

End Module
JedeyeAsked:
Who is Participating?
 
Deepak LakkadCommented:
Hi,

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

- Deepak Lakkad
0
 
Deepak LakkadCommented:

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

0
 
Deepak LakkadCommented:
Hi

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

- Deepak Lakkad
0
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.