Q: Exception class that inherits from system.dividebyzeroexception I have created a inherited class named ZeroException that inherits from System.DivideByZero Class but when a dividebyzero exception occurs it doesnt catch the event.
Class ZeroException
Inherits System.DivideByZero
Public Overrides ReadOnly Property Message() as String
Get 
Return "Divide By Zero"
End Get
End Property
End Class

Main Code:
Sub Calc()
dim x as integer=5
dim y as integer=0
dim k as single

Try   
k=x\y 'Integer Division
Catch ex as ZeroException  
Console.WriteLine(ex.Message) 'But I get another message. Not Divide By Zero
End Try

End Sub


A: you dont catch a custom exception but rather you throw one.. so in this case your code should look like follows:
    Try
        k = x \ y 'Integer Division
    Catch ex As DivideByZeroException
        Throw New ZeroException
    End Try

Here is a test case that demonstrates this.
Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting

Class ZeroException
    Inherits DivideByZeroException
    Public Overrides ReadOnly Property Message() As String
        Get
            Return "Divide By Zero"
        End Get
    End Property
End Class

<TestClass()> Public Class UnitTest1

    <TestMethod()> Public Sub TestMethod1()
        Try
            MethodThrowsCustomException()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

    Public Sub MethodThrowsCustomException()
        Dim x As Integer = 5
        Dim y As Integer = 0
        Dim k As Single

        Try
            k = x \ y 'Integer Division
        Catch ex As DivideByZeroException
            Throw New ZeroException
        End Try

    End Sub

End Class

