|
-
Jun 16th, 2003, 06:41 AM
#1
Thread Starter
Junior Member
Error Handling class?
Hi there,
I am trying to write a simple error handling class and I can't seem to find any kind of samples on the web. I am catching the error like so...(errorhandling being my errorhandling class object)
Code:
'class variables
Dim errorString As String
Dim exh As errorhandling
Public Sub....
.....'code
.......
Catch ex As Exception
trans.Rollback()
exh = CType(context.Handler, errorhandling)
'handle(ByVal ex as Exception) is the function which the ecception is passed to in the err handling class
errorString = exh.handle(ex)
Response.Redirect("Error.aspx?errorMsg=" & errorString)
Finally
If myConn.State = ConnectionState.Open Then
myConn.Close()
End If
myConn = Nothing
End Try
When I run it as is gives me a casting error with my ctype conversion...?? I can't even get into my error handling class to check if it works! Please, what am I missing here?
Another thing I'm trying to figure out is how I can access the specific error number? I know that Exceptions have a HResult() method but it's protected and I'm not sure how else I can access the values.
Any help on these issues would be most appreciated.
Cheers.
-
Jun 16th, 2003, 08:25 AM
#2
Sleep mode
Try this
VB Code:
Dim errorString As String
Dim exh As errorhandling
try
Catch ex As Exception
trans.Rollback()
exh = CType(context.Handler, errorhandling)
'handle(ByVal ex as Exception) is the function which the ecception is passed to in the err handling class
errorString = exh.handle(ex)
Response.Redirect("Error.aspx?errorMsg=" & errorString)
'it should be here to catch the error if raised .
[B]catch x as execption [/B]
If myConn.State = ConnectionState.Open Then
myConn.Close()
End If
myConn = Nothing
messagebox.show (x.message)
-
Jun 16th, 2003, 09:07 AM
#3
Thread Starter
Junior Member
On inserting that, it gets stuck when I try to call the handle() method in my error handling class:
(where errorString is a string looking for a return error string from the errorhandling class and exh is an instance of the errorhandling class)
Code:
errorString = exh.handle(ex)
...it gives me the error:
"Object reference not set to an instance of an object"
-
Jun 16th, 2003, 10:21 AM
#4
Sleep mode
Try this now :
VB Code:
try
'Remove this and rebuild the try structure !
Catch ex As Exception
trans.Rollback()
exh = CType(context.Handler, errorhandling)
'handle(ByVal ex as Exception) is the function which the ecception is passed to in the err handling class
errorString = exh.handle(ex)
Response.Redirect("Error.aspx?errorMsg=" & errorString)
'it should be here to catch the error if raised .
catch x as execption
Dim errorString As String
Dim exh As errorhandling
If myConn.State = ConnectionState.Open Then
myConn.Close()
End If
myConn = Nothing
messagebox.show (x.message)
-
Jun 16th, 2003, 10:43 AM
#5
Thread Starter
Junior Member
I'm so dozy I just forgot to instantiate the errorhandling class object. I have since done this and can now access the class...but I don't suppose you could point me in the right direction as to how I can access the error codes (I want to use a Select statement and firstly look for which exception occured and secondly, the different types of SQLExceptions etc.) and send back a manipulated string according to which kind of error it is?
Much thanks for your help.
-
Jun 16th, 2003, 10:51 AM
#6
Sleep mode
Oops .
For detailed info about generated errors , you can access different functions of the System.Exception class . Like :
Dim Exception as Exception
Exception.Stack
Exception.Source
Exception.Site
Exception.Message
then you can combine them all togother and show up a messagebox or write them to log file . Is this what you're asking about ?
-
Jun 16th, 2003, 11:35 AM
#7
Thread Starter
Junior Member
Not quite. I was under the impression that it was possible to get the associated error number of an error (just as a simple example...
[code]
Public Function handle(ByVal ex As Exception)
Dim exNo As Integer
Select Case exNo
Case 111
if exno = 1
Return "An SQL Exception has occured. Type:"
Case 222
Return "An IO Exceptoion has occured. Type:"
Case Else
s = ex.ToString
Return s
End Select
End Function
-
Jun 16th, 2003, 11:38 AM
#8
Sleep mode
As far as I know , there is no associated number with exception error in .NET .
-
Jun 16th, 2003, 11:45 AM
#9
Thread Starter
Junior Member
Not quite. I was under the impression that it was possible to get the associated error number of an error.
An example of the idea I'm trying to work out...
Code:
Public Function handle(ByVal ex As Exception)
Dim exNo As Integer
Dim s As String
exNo = ex.HResult()
s = ex.ToString
Select Case exNo
Case 111
If exNo = 1 Then
Return "An SQL Null Value Exception has occured. Type:" & s
ElseIf exNo = 2 Then
Return "An SQL Exception has occured. Type:" & s
End If
Case 222
Return "An IO Exception has occured. Type:" & s
Case Else
s = ex.ToString
Return s
End Select
End Function
But HResult is a protected method of the Exception class...is there not some way I can access this to find out the specific error numbers?
-
Jun 16th, 2003, 12:01 PM
#10
Sleep mode
Since you are working on handling specific exception "SQL Exception" , why don't you use it instead of the generic exception class ? Like this :
This gets the error number I guess .
VB Code:
Catch ex As SqlClient.SqlException
MessageBox.Show(ex.Number())
and so on .
-
Jun 17th, 2003, 05:11 AM
#11
Thread Starter
Junior Member
because I don't know what type of error has occured until it reaches my errorhandling class!
Basically I want to catch whatever error occurs, pass it to a my errorhandling class, figure out what type it is and return an appropriate string back to where the error occured.
How can I figure out what type of error has occured?
-
Jun 17th, 2003, 07:03 AM
#12
Fanatic Member
SQLExceptions have associated error numbers. For example, error number 229 is a permissions error. You can access it by myException.Number where myException is a SQLException. Regular Exception types don't have a number associated.
I also trap specific SQL Exception Numbers in some of my code to take the appropriate action. Works well for me!
-
Jun 17th, 2003, 08:32 AM
#13
Sleep mode
Originally posted by kto
How can I figure out what type of error has occured?
Call this little sub to see what type of exception it's going to throw . This is one way .
VB Code:
Private Sub callme()
Dim errortype As String
Dim ex As Exception
Try
Dim a As Integer = "aaaa"
Catch ex
'here you can figure out what error was generated
'simply by converting exception type to
'string
errortype = ex.GetType().ToString
MessageBox.Show(errortype.ToString)
End Try
End Sub
-
Jun 17th, 2003, 08:42 AM
#14
Sleep mode
This is the other way . It uses GetType
VB Code:
Private Sub callme()
Dim errortype As String
Dim ex As Exception
Try
Dim a As Integer = "aaaa"
Catch ex
End Try
'here you have to list exceptions that are expect to be thrown
'in this way .
If ex.GetType Is GetType(InvalidCastException) Then
MsgBox("Error Type " & ex.Message & "Do whatever here ")
ElseIf ex.GetType Is GetType(ExecutionEngineException) Then
'do your code here
ElseIf ex.GetType Is GetType(InvalidProgramException) Then
'do your code here
End If
End Sub
-
Jun 17th, 2003, 11:14 AM
#15
Thread Starter
Junior Member
cheers mate, you've made this wee Irish lass very happy!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|