Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27382

VS 2010 Constructors and exceptions

$
0
0
Hi Folks

I am struggling along with my first few classes and some custom exceptions. If I have the following:

vb Code:
  1. Dim currentDuts As New Dictionary(Of Integer, DeviceUnderTest)
  2. Dim newDutKey As Integer = currentDuts.Count + 1
  3. Try
  4.     currentDuts.Add(newDutKey, New DeviceUnderTest(LocationBox.Text))
  5. Catch ex As Exception
  6. '...
  7. End Try

A) What happens to the newly created DeviceUnderTest object if currentDuts.Add() fails, is it destroyed again, or is it floating somewhere in memory?

B) For that matter what happens if New DeviceUnderTest() fails? Does .Add() also fail? I'm guessing yes, but in that case, if I want to filter the exceptions by type, which exception will 'ex' be, the one from New() or the one from .Add()?

Also;

C) Should the tests on a constructors arguments be before or after MyBase.New(), I assume if the tests are after, any exceptions they cause to be thrown would not have prevented the object from being created... Is that the case? (Or maybe it's totally irrelevant?)

i.e.
vb Code:
  1. Public Class DeviceUnderTest
  2.     Public Sub New(argument As String)
  3.         'Should MyBase.New() go here...
  4.         If argument = wrong Then
  5.             Throw New wrongArgumentException
  6.         Else
  7.             myPrivateVar = argument
  8.         End If
  9.         'Or should MyBase.New() go here?
  10.     End Sub
  11.     '...
  12. End Class

Thanks

Viewing all articles
Browse latest Browse all 27382

Trending Articles