M
Mike Hofer
Hi guys,
Long ago, I learned that it was a bad idea to throw exceptions inside a
constructor. For instance:
Public Class OutCountMovementArgs
Private innerDirection As OutCountDirection
Private innerStartDate As Date
Private innerEndDate As Date
Public Sub New(ByVal direction As OutCountDirection, _
ByVal startDate As Date, _
ByVal endDate As Date)
If startDate < #10/1/2004# Or startDate > Today Then
Throw New DateOutOfRangeException("startDate")
End If
If endDate < #10/1/2004# Or endDate > Today Then
Throw New DateOutOfRangeException("endDate")
End If
If startDate > endDate Then
Throw New DateRangeOutOfOrderException
End If
innerDirection = direction
innerStartDate = startDate
innerEndDate = endDate
End Sub
Public ReadOnly Property Direction As OutCountDirection
Get
Return innerDirection
End Get
End Property
Public ReadOnly Property StartDate As Date
Get
Return innerStartDate
End Get
End Property
Public ReadOnly Property EndDate As Date
Get
Return innerEndDate
End Get
End Property
End Class
Is this still considered a bad idea? My gut tells me that it is, as a
lot of users tend to write variable initialization code outside of
Try-Catch blocks, like this:
Dim myStartDate As Date
Dim myEndDate As Date = Today
Dim args As New OutCountArgs(OutCountDirection.Into, myStartDate,
myEndDate)
Try
Long ago, I learned that it was a bad idea to throw exceptions inside a
constructor. For instance:
Public Class OutCountMovementArgs
Private innerDirection As OutCountDirection
Private innerStartDate As Date
Private innerEndDate As Date
Public Sub New(ByVal direction As OutCountDirection, _
ByVal startDate As Date, _
ByVal endDate As Date)
If startDate < #10/1/2004# Or startDate > Today Then
Throw New DateOutOfRangeException("startDate")
End If
If endDate < #10/1/2004# Or endDate > Today Then
Throw New DateOutOfRangeException("endDate")
End If
If startDate > endDate Then
Throw New DateRangeOutOfOrderException
End If
innerDirection = direction
innerStartDate = startDate
innerEndDate = endDate
End Sub
Public ReadOnly Property Direction As OutCountDirection
Get
Return innerDirection
End Get
End Property
Public ReadOnly Property StartDate As Date
Get
Return innerStartDate
End Get
End Property
Public ReadOnly Property EndDate As Date
Get
Return innerEndDate
End Get
End Property
End Class
Is this still considered a bad idea? My gut tells me that it is, as a
lot of users tend to write variable initialization code outside of
Try-Catch blocks, like this:
Dim myStartDate As Date
Dim myEndDate As Date = Today
Dim args As New OutCountArgs(OutCountDirection.Into, myStartDate,
myEndDate)
Try