isolating a particular exception

G

Guest

I'm checking whether an exception is a DBConcurrencyException successfully but inelegantly in the code snippet below
I tried to capture the hresult property and compare that way, but ex.hresult says it is not accessible because it is a protected property. Is there a more elegant way of capturing a particular exception

Unlike oledbexceptions, exception has no SQLState property, so I can't compare them that way


Catch ex As Exceptio
If ex.GetType.ToString = "System.Data.DBConcurrencyException" The
MsgBox("HRESULT = " & s & ex.hresult

polynomial5d
 
M

Marina

Yes, more then one catch clause:

Catch dbEX As DBConcurrencyException
' do suff specific to DBConcurrencyException here
Catch ex As Exception
'do stuff for all other exceptions here

polynomial5d said:
I'm checking whether an exception is a DBConcurrencyException successfully
but inelegantly in the code snippet below.
I tried to capture the hresult property and compare that way, but
ex.hresult says it is not accessible because it is a protected property. Is
there a more elegant way of capturing a particular exception?
Unlike oledbexceptions, exception has no SQLState property, so I can't compare them that way.



Catch ex As Exception
If ex.GetType.ToString =
"System.Data.DBConcurrencyException" Then
 
P

Patrice

See the type of the exception as the "family". If those exceptions are all
from the same "family", then you have no other solution than testing one of
the properties to see what is the exact case you encountered.

In some case you could also prevent exceptions of happening (for example
checking for key existance before inserting) and catching the exception is
then mostly to report an unexpected problem to the developer...

Patrice

polynomial5d said:
but what if the exception you want to catch doesn't have a 'name'. For
example, the duplicate key oledbexception whose sqlstate is 3022. I have to
weed that one out with an if..then..else statement. If ex.sqlstate = 3022
then...etc.
 
P

Patrice

See the doc for this exception. It inherits ("is a") from
System.SystemException that inherits itself from System.Exception. That is
it is a System.Exception and is then treated in the first clause that match
(that is the last one).

If you ask yourself about the design, DB concurrency exceptions are not
particular to a given data source provider. This is probably why it doesn't
inherit from OleDbException.

Patrice

polynomial5d said:
OK, I've made progress thanks to you guys. Now I want to go one step
farther. I haven't cleaned up the subroutine yet, so the following snippet
is ugly. Please believe that I'll clean it up after I have it all settled.
In the try, you'll see an attempt to catch oledb exceptions. The first
one is 3022, the else is everything else. But when I have a
DBConcurrencyException, it's not being caught in the else. Later I'm
catching all exceptions, and that's where the DBConcurrencyException is
being caught.
I'd really like to know why the DB... isn't being caught in the first else clause.

polynomial5d


Private Sub btnSubmitChanges_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSubmitChanges.Click
Dim Check As Integer = 0
If DsDT1.HasChanges Then
DTypeOpened = False
Try
Dim intModified As Integer
intModified = daDT.Update(DsDT1.DateType)
Dim strOutput As String
strOutput = "Modified " & intModified & " item(s)"
MessageBox.Show(strOutput, "Update succeeded!",
MessageBoxButtons.OK, MessageBoxIcon.Information)
DTypeOpened = True
Dim s = ControlChars.CrLf
Catch ex As OleDbException
If ex.Errors(0).SQLState = 3022 Then
MsgBox(ex.Errors(0).Message & s & "Please try again")
Check = 1
Exit Try
Else
Dim errorMessages As String
Dim i As Integer
For i = 0 To ex.Errors.Count - 1
errorMessages += "Index #" & i.ToString() & ControlChars.CrLf _
& "Message: " & ex.Errors(i).Message & ControlChars.CrLf _
& "NativeError: " &
ex.Errors(i).NativeError & ControlChars.CrLf _
& "Source: " & ex.Errors(i).Source & ControlChars.CrLf _
& "SQLState: " &
ex.Errors(i).SQLState & ControlChars.CrLf _
& "The form will be closed"
Check = 2
Next i
MsgBox(errorMessages)
If Check = 2 Then Me.Close()
End If
Catch ex As DBConcurrencyException
MsgBox(ex.Message & s & "The dataset will be refreshed." &
s & "Then you can navigate to the row and update it again.")
DsDT1.Clear()
daDT.Fill(DsDT1, "DateType")
Exit Try
Catch ex As Exception
MsgBox(ex.GetType.ToString & s & ex.Message & s &
ex.HelpLink & s & ex.StackTrace & s & ex.Source & s & "The form will be
closed") '& s & ex.TargetSite)
Check = 2
Me.Close()

End Try
Else
MessageBox.Show("No changes to submit!", "SubmitChanges",
MessageBoxButtons.OK, MessageBoxIcon.Information)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top