Weird problem with exception constructor

  • Thread starter Thread starter Versteijn
  • Start date Start date
V

Versteijn

Hello all,

I have a custom Exception inherited class with the following
constructor. I added the IIf call because the Parameters() value might
be null. But somehow I keep getting an ArgumentNullException on
String.Format: 'Value cannot be null. Parameter name: args'.
If I replace this FalsePart of IIf with a random string like "hello
world", nothing is wrong, the correct part (TruePart) is used and I
get no exception.
It looks like the String.Format is executed anyway, also when the IIf
returns TruePart.

Any clues?

Regards,

Freek Versteijn


Public Sub New(ByVal ExceptionID As Integer, ByVal Parameters() As
Object, ByVal Message As String, ByVal InnerException As Exception)
MyBase.New(IIf(Parameters Is Nothing, Message,
String.Format(Message, Parameters)) & " (ExceptionID=" & ExceptionID &
")", InnerException)
FExceptionID = ExceptionID
FParameters = New Collection
End Sub
 
Hi,

Make more than one version of the new procedure. Make one version
without the parameters option.

Public Sub New(ByVal ExceptionID As Integer, ByVal Parameters() As
Object, ByVal Message As String, ByVal InnerException As Exception)

MyBase.New(String.Format(Message, Parameters) & " (ExceptionID="
& ExceptionID & ")", InnerException)
FExceptionId = ExceptionID
FParameters = New Collection
End Sub

Public Sub New(ByVal ExceptionID As Integer, ByVal Message As
String, ByVal InnerException As Exception)

MyBase.New(Message & " (ExceptionID=" & ExceptionID & ")",
InnerException)
FExceptionId = ExceptionID
FParameters = New Collection
End Sub

Ken
------------------
 
Versteijn,
As Ken stated overload the constructor & make two versions.
It looks like the String.Format is executed anyway, also when the IIf
returns TruePart.

Any clues?
The reason for the exception is that IIf is a function! Both the true part &
false part are always evaluated! All three parameters are passed to the IIf
function which then returns one of the two values.

Hope this helps
Jay
 
Jay B. Harlow said:
Versteijn,
As Ken stated overload the constructor & make two versions.

The reason for the exception is that IIf is a function! Both the true part &
false part are always evaluated! All three parameters are passed to the IIf
function which then returns one of the two values.

Hope this helps
Jay

DOH! It must have been very very late. Your're right. This is
problably the most stupid problem I have ever had.. Shame:(

Regards,

Freek Versteijn
 
Back
Top