[RESOLVED] Err.Number (Can it be done in .NET 2005)?
Hi all,
In VB6, you could put into an error prompt the error number using err.number. Does 2005 support doing this?
When an error occurrs in my app and I have handled the exceptions, I want to be able to display what line the error occurred on.
Please advise.
Re: Err.Number (Can it be done in .NET 2005)?
Yes, use the Exception object if you are Catching on it.
Code:
MyException.Errors.Item(0).Number.ToString
Re: Err.Number (Can it be done in .NET 2005)?
err.number is the error's ID number, not the line it occured in. This is true in VB6 and .NET.
Re: Err.Number (Can it be done in .NET 2005)?
Correct, Erl is in VB 6 the error line number object of you have line numbering in your procedure. Otherwise what I posted will get the line number in VB.NET
Re: Err.Number (Can it be done in .NET 2005)?
Rob, what am I overlooking?
exceptions don't have an errors collection, so what datatype is MyException in your example code?
Re: Err.Number (Can it be done in .NET 2005)?
Oh Snap! That was in 2003. Heres the 2005 version.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim o As Object
Try
MessageBox.Show(o.ToString)
Catch ex As Exception
Dim sMess As String = ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(":"))
MessageBox.Show("Error at" & sMess)
End Try
End Sub
End Class
Now you could improve upon it and take into account that you will have multiple errors sometimes or whatever you want to do.
Re: Err.Number (Can it be done in .NET 2005)?
This modification will get the first error only. The previous one was getting the last error line number when you have multiple errors.
Code:
Dim sMess As String = ex.StackTrace.Substring(ex.StackTrace.IndexOf(":", ex.StackTrace.IndexOf(":") + 1))
MessageBox.Show("Error at" & sMess)
Re: Err.Number (Can it be done in .NET 2005)?
Rob for my own sanity, did the exception class in 2003 have an errors property? I don't remember that it did. I know some of the special exceptions had an errors collection, like a SQLException.
I just want to make sure my knowledge is correct ;)
Re: Err.Number (Can it be done in .NET 2005)?
Yes, I pulled that code from one project where I was catching errors from the SQLException collection. I didnt realize they werent common to all exceptions nor in 2005. :(
Re: Err.Number (Can it be done in .NET 2005)?
Re: Err.Number (Can it be done in .NET 2005)?
Yes, thats the old VB 6 Microsoft.Visualbasic class. You will have to manually add all the number lines to do it though.
Post #7 wil work without numbering..
Re: Err.Number (Can it be done in .NET 2005)?
Quote:
Originally Posted by RobDog888
Yes, thats the old VB 6 Microsoft.Visualbasic class. You will have to manually add all the number lines to do it though.
Post #7 wil work without numbering..
i tried that MSDN article and i added the line numbers yet it returned a line number of 0 but the err.raise was on line 8, so that definately didnt work
Re: Err.Number (Can it be done in .NET 2005)?
when i do:
vb Code:
"Error Line: " & ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(":")) & vbNewLine & _
it shows as "Error Line: :line 22
What I would like for it to do is "Error Line: 22"
Can this be done?
Quote:
Originally Posted by RobDog888
Oh Snap! That was in 2003. Heres the 2005 version.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim o As Object
Try
MessageBox.Show(o.ToString)
Catch ex As Exception
Dim sMess As String = ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(":"))
MessageBox.Show("Error at" & sMess)
End Try
End Sub
End Class
Now you could improve upon it and take into account that you will have multiple errors sometimes or whatever you want to do.
Re: Err.Number (Can it be done in .NET 2005)?
i did this: err.erl (and had to add line numbers) and it worked well
but then i get a ContextSwitchDeadLock error on my messagebox showing the error generated
Re: Err.Number (Can it be done in .NET 2005)?
Use the code from post #7. You are only using part of it.
Re: Err.Number (Can it be done in .NET 2005)?
Quote:
Originally Posted by RobDog888
Use the code from post #7. You are only using part of it.
when using that code:
it shows as "Error Line: :line 22
What I would like for it to do is "Error Line: 22"
how can I do that?
Re: Err.Number (Can it be done in .NET 2005)?
You didnt copy all of it as you are missing the "+1 )" at the end. Hence the reason for the double colons.
Copied from #7
Quote:
Dim sMess As String = ex.StackTrace.Substring(ex.StackTrace.IndexOf(":", ex.StackTrace.IndexOf(":") + 1))
MessageBox.Show("Error Line: " & sMess)
Re: Err.Number (Can it be done in .NET 2005)?
Quote:
Originally Posted by RobDog888
You didnt copy all of it as you are missing the "+1 )" at the end. Hence the reason for the double colons.
Copied from #7
even when i do that, it shows
Error Line: :line #
Re: Err.Number (Can it be done in .NET 2005)?
Its just string parsing. Seems it needs another +1 for the starting position of the returned string.
Code:
Dim sMess As String = ex.StackTrace.Substring(ex.StackTrace.IndexOf(":", ex.StackTrace.IndexOf(":") + 1) + 1)
MessageBox.Show("Error Line: " & sMess)
Re: Err.Number (Can it be done in .NET 2005)?
adding +5 instead of +1 got rid of :line :)
thanks for your help RD