Question guidelines to handle exceptions in a class

enzom83

Member
Joined
Nov 28, 2005
Messages
18
Programming Experience
5-10
I created a custom collection class with some methods: each of these has some parameters and throws an exception if a parameter is not valid.
For example, the Add and Remove methods throw an NullReferenceException if one of their parameters is null.
VB.NET:
Sub Add(ByRef word As String)
    If (word Is Nothing) Then
        Throw New NullReferenceException("word must be not null")
    End If
    ...
End Sub

Sub Remove(ByRef word As String)
    If (word Is Nothing) Then
        Throw New NullReferenceException("word must be not null")
    End If
    ...
End Sub

Sub Replace(ByRef word1 As String, ByRef word2 As String)
    Remove(word1)
    Add(word2)
End Sub

The Replace method replaces an item in the collection with another specified object: to reach the goal, it first calls the Remove method and after the Add method. Obviously the Replace method could throw exceptions if at least one of the specified parameters is null, because Remove and Add methods could throw exceptions.
Is this the right way to go? Or should I also check the parameters in the Replace method as in the following code?
VB.NET:
Sub Replace(ByRef word1 As String, ByRef word2 As String)
    If (word1 Is Nothing) Then
         Throw New NullReferenceException("word1 must be not null")
    End If
    If (word2 Is Nothing) Then
        Throw New NullReferenceException("word2 must be not null")
    End If
    Remove(word1)
    Add(word2)
End Sub
In fact, in this second case, each check would be executed twice: first in the Replace method and after in the Remove and Add methods, which are called by Replace, and in other cases the checks may be rather expensive.

Thanks a lot for any advice!
 
Last edited:
First up, that's the wrong exception. You should never be throwing a NullReferenceException. That is for the system to throw if you try to access a member of an object via a null reference. In your case, you should be throwing an ArgumentNullException, which is for when null is past to a parameter that is not allowed to be null.

Secondly, there is absolutely no reason for your method parameters to be declared ByRef. ByRef is only for when you want to pass out a different value than you passed in. ByVal should be the default and you should only ever use ByRef is you specifically need to.

As for your custom collection, I'm sorry to say that you're almost certainly doing it wrong. You should not have any of those three methods. In almost all cases, the proper way to implement a custom collection is to inherit the System.Collections.ObjectModel.Collection(Of T) class, with T being String in your case. You then override the appropriate Protected members to add custom functionality. The methods to override are ClearItems, InsertItem, RemoveItem and SetItem. These methods are implicitly called whenever the following members are accessed:

ClearItems: Clear method
InsertItem: Add method, Insert method
RemoveItem: Remove method, RemoveAt method
SetItem: Item property setter

It's in those methods that you would check for null arguments and throw ArgumentNullExceptions.
 
Back
Top