Help with throwing Custom Exceptions

superservo15

Member
Joined
Apr 25, 2006
Messages
11
Location
Canada
Programming Experience
10+
Hi All,
I'm having problems trying to "bubble" up a custom exception I made... And I'm pretty new to custome exceptions, I was just told to use it this way and figure out the implementation.

I have a custom exception class called "SystemIsBusyException"

VB.NET:
Public Class SystemIsBusyException
    Inherits System.Exception

       Public Sub New()
        MyBase.New()
    End Sub

 End Class

In my data layer class I do a check to see if the system is busy (ie/ a status is set to a certain value). If it is, I want to throw my custom exception back to my business object, then in the business object, throw it back to my controller class, where the error will be handled on the UI.

My DLC looks like this
VB.NET:
Try
If OurSystem.IsBusy Then
      Throw New SystemIsBusyException()
End If
'DO DLC stuff

Catch ex As SystemIsBusyException
     Throw ex
Catch ex As Exception
      Throw ex
 End Try

Then in my Business Object I also have a try catch like

VB.NET:
   'Call DLC
Catch ex As SystemIsBusyException
     Throw ex 
Catch ex As Exception
     Throw ex
End Try

When it hits either of the exception handlers, I get a "SystemIsBusyException was Unhandled" error with the message being
"Exception of type 'Reins.Annuity.SystemIsBusyException' was thrown."

I can't figure out how to bubble up my custom exception like we do with regular system and app exceptions and I can't figure out what I am missing to make it happen.

Any help is MUCH appreciated, thanks :)
 
If you're not going to do anything with the exception you shouldn't catch it and let it bubble up on its own.

If you do want to perform some operation on it and then re-throw it you'll need to take some considerations to save the full stack call trace.

VB.NET:
Private Shared Sub PreserveStackTrace(ByVal exception As Exception)
    Dim preserveStackTrace As MethodInfo =
        GetType(Exception).GetMethod("InternalePreserveStackTrace", BindingFlags.Instance Or BindingFlags.Public)
    preserveStackTrace.Invoke(exception, Nothing)
End Sub

Based off of this article: Rethrowing exceptions and preserving the full call stack trace - Fabrice's weblog

As for catching a custom exception I had no problems in this simple example:

VB.NET:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim answer = Divide(10, 0)
        Catch tce As TestCustomException
            MessageBox.Show(tce.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Shared Function Divide(ByVal x As Double, ByVal y As Double) As Double
        If y = 0 Then
            Throw New TestCustomException("Can't Divide By Zero")
        End If
        Return x / y
    End Function

End Class

Public Class TestCustomException
    Inherits System.Exception
    Public Sub New(ByVal message As String)
        MyBase.New(message)
    End Sub
End Class
 
Ha turns out my exception was not being caught atthe top level.. another developer added in a save call I wasn't aware of, so the exception literally was unhandled. That took a stupid amount of time to figure out for something so easy...

Thanks for the reply and info. I don't need to preserve the stack trace, but it's handy to know and I have bookmarked the link :)
 
Last edited:
If you do want to perform some operation on it and then re-throw it you'll need to take some considerations to save the full stack call trace.
If you are just rethrowing you can use empty Throw to preserve stack trace (re-thrower is added to stacktrace), or not catch the exception. Throw Statement (Visual Basic)
Catch ex As SystemIsBusyException
Throw ex
Catch ex As Exception
Throw ex
Note that System.Exception is a catch all, it includes SystemIsBusyException, but the recommendation is actually to not catch the System.Exception type (except in top level code).
 
Back
Top