T
Tyrant Mikey
Question for all you MVPs:
Is the Catch block required in a Try...Catch...Finally block? If not,
what happens when an exception occurs inside a Try block?
For instance, consider the following code:
Public Shared Function Load(ByVal censusDate As Date) As Census
Dim proc As New
SelectCensusByDateProcedure(Common.GetOpenConnection())
Dim row As New Census
Dim rdr As SqlDataReader
Try
proc.CensusDate = censusDate
rdr = proc.ExecuteReader()
If rdr.Read Then
row = New Census(rdr)
End If
Finally
If Not rdr Is Nothing Then
rdr.Close()
End If
proc.Dispose()
End Try
Return row
End Function
I have a guess as to what happens, based on observation. I think that
if an exception occurs in the Try block, the Finally block is
immediately called, and then the exception is raised to the caller.
Can someone please verify that this is valid, and whether or not it's
an acceptable approach? I really hate having to write the following:
Try
proc.CensusDate = censusDate
rdr = proc.ExecuteReader()
If rdr.Read Then
row = New Census(rdr)
End If
Catch ex As Exception
Throw ex
Finally
If Not rdr Is Nothing Then
rdr.Close()
End If
proc.Dispose()
End Try
I did it for a long time because I didn't know for sure if it was safe
to omit the Catch block. I'd like some confirmation that it's okay.
Thanks.
Is the Catch block required in a Try...Catch...Finally block? If not,
what happens when an exception occurs inside a Try block?
For instance, consider the following code:
Public Shared Function Load(ByVal censusDate As Date) As Census
Dim proc As New
SelectCensusByDateProcedure(Common.GetOpenConnection())
Dim row As New Census
Dim rdr As SqlDataReader
Try
proc.CensusDate = censusDate
rdr = proc.ExecuteReader()
If rdr.Read Then
row = New Census(rdr)
End If
Finally
If Not rdr Is Nothing Then
rdr.Close()
End If
proc.Dispose()
End Try
Return row
End Function
I have a guess as to what happens, based on observation. I think that
if an exception occurs in the Try block, the Finally block is
immediately called, and then the exception is raised to the caller.
Can someone please verify that this is valid, and whether or not it's
an acceptable approach? I really hate having to write the following:
Try
proc.CensusDate = censusDate
rdr = proc.ExecuteReader()
If rdr.Read Then
row = New Census(rdr)
End If
Catch ex As Exception
Throw ex
Finally
If Not rdr Is Nothing Then
rdr.Close()
End If
proc.Dispose()
End Try
I did it for a long time because I didn't know for sure if it was safe
to omit the Catch block. I'd like some confirmation that it's okay.
Thanks.