How to isolate a particular exception

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
=?Utf-8?B?cG9seW5vbWlhbDVk?= said:
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
MsgBox("HRESULT = " & s & ex.hresult)

C#:
try
{
// ...
}
catch (System.Data.DBConcurrencyException dbe)
{
MessageBox.Show(dbe.Message)
}

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp


Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
you can use

more than one catch clause in your app. Exampe (sorry this is in c# but the
same applies to vb.net):

try
{
....
}
catch(DBConcurrencyException ex)
{
.. do something
}
catch(Exception ex)
{
.. do something else
}


hope this helps

Fitim Skenderi
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
 
polynomial5d said:
thank both of you. It works well enough.

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.
How do I do that one elegantly?

That's about as elegant as it gets!

I don't recall if you're using VB.NET, but if you are, its Catch statement
has an enhancement over that in C#:

Try
....
Catch sqlEx as SqlException When sqlEx.Number = 3022
....
End Try
 
My documentation shows OleDbError.SQLState to be a string.

I recommend that you turn on Strict mode in your project properties pages.
It will catch errors like this at compile-time.
--
John Saunders
John.Saunders at SurfControl.com



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)
 
Yes, John. However 3022 is nevertheless caught. It's dbconcurrencyexception that's not caught in the else. That's my problem. You're right about the strict, however, will do

polynomial5d
 
"Is there a more elegant way of capturing a particular exception?"

If it is a protected exception, you could obtain it through inheritance.


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top