Results 1 to 25 of 25

Thread: [RESOLVED] Constructors and exceptions

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Resolved [RESOLVED] Constructors and exceptions

    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
    Last edited by wolf99; Jul 23rd, 2013 at 03:12 PM.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width