Custom exceptions
Besides finishing my study I'm currently also working as a software developer. And yesterday I came across a problem which thought me how a lesson.
The project I was working is basically a custom import tool for a financial software. We get an CSV or an XML file and that is imported using the SDK of the financial software. Nothing hard except that the SDK is unfinished and various features are yet to be implemented. The majority does work, but it is like walking through a mine field and every time you move forward the application might blow up in your face
The application is written in Visual Basic .NET (not my choice, but it does the job) and the SDK uses exceptions to notify that a certain feature is not working or when a certain object doesn't meet certain requirements when you ask it to be saved.
Because of the huge amount of data, I used reflection so that I don't have to type as much. My system also uses exceptions, and I won't be surprised if you already where this is going, but while I was debugging I came across multiple exceptions of the SDK which meant that I either had to write a workaround for it or remove the feature until the SDK does support it. When I finally fixed the majority of the errors I suddenly got hit in the face by the following exception:
[cc_text]Unable to complete save[/cc_text]
Now the SDK did provide cryptic error messages but this one was completely new and I was doing a bare bone test. I called a coworker over to see if he knew what it meant but he also didn't know what the message meant. A few minutes later he came back to me and said that the part of the SDK did work on his side and that the error must be somewhere in my code.
To cut a long story short. The exception that I got was my own and I hadn't noticed because I had already encountered so many exceptions of the SDK that I just presumed that it was again the SDK that was throwing a fit.
Once I realized that it didn't take long to solve it (I returned a true instead of false somewhere), however I was more worried about how to prevent it in the future. After some thought that also was simple.
If any of the application code would throw a fit it would throw not the standard exception which would have been:
[cc_vb]throw new Exception("Unable to complete save")[/cc_vb] but a custom type and a search and replace later we got:
[cc_vb]throw new UtilityException("Unable to complete save")[/cc_vb] so that error message would contain a special prefix by which it would be clear that it was our own code that decided to throw an exception.
[cc_vb]
Public Class UtilityException
Inherits System.Exception
Public Sub New(ByVal str As String)
MyBase.new(str)
End Sub
Public Overrides ReadOnly Property Message() As String
Get
Return "UtilityException: " & MyBase.Message
End Get
End Property
End Class
[/cc_vb]
In the future we will know when it was our own mine to blew up. A simple solution but it would have saved us a lot of time if we had done this from the start.