quinta-feira, 12 de abril de 2012 12:58
In my windows application, I want to check which part of code (or routine) has caused un-handled exception event to occur from application events in its running mode (not debugging).
If this is possible then please guide me how to do this.
If any clarification regarding this is required, please ask.
Todas as Respostas
quinta-feira, 12 de abril de 2012 13:22
"Application events" means handling Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.UnhandledException?
In the handler, you get all information via the parameters, for example e.Exception contains the exception, consequently e.exception.stacktrace contains the stack trace. Display it or better write it to a file.
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException IO.File.WriteAllText("d:\log " & DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss.ff") & ".txt", e.Exception.ToString) End Sub
- Editado Armin Zingler quinta-feira, 12 de abril de 2012 13:24
- Sugerido como Resposta Reed KimbleMicrosoft Community Contributor, Moderator sexta-feira, 13 de abril de 2012 20:35
quinta-feira, 12 de abril de 2012 13:24By running mode.. since you are not talking about debugging, what do you mean? The published exe file (in bin\Debug)?
If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
Check out my development so far!
quinta-feira, 12 de abril de 2012 13:58
If you have used this kind of code (and that is where it often appears) then the answer is no.
Then handle first all the catches.
- Editado Cor LigthertMVP quinta-feira, 12 de abril de 2012 13:59
quinta-feira, 12 de abril de 2012 17:13
Thanks Armin!I think your answer would help me to solve my problem. Let me check it.
quinta-feira, 12 de abril de 2012 17:15
Thanks for replying bilde.
exactly, by running mode I mean published exe file.
quinta-feira, 12 de abril de 2012 17:20
I want to track the source which has caused unhandled exception event to occur from application events, not from try/catch statement.
quinta-feira, 12 de abril de 2012 18:27
There are two ways, put in all the situations that an exception can be thrown a try catch (you even can do that around a complete class and try to find in what class the exception occurs.
You can also include a tracer routine in your program which writes everytime which method is done then you see which one has thrown the event.
quinta-feira, 12 de abril de 2012 19:45
What's wrong with a breakpoint?
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
sexta-feira, 13 de abril de 2012 10:48
I think it would be a better option to include a tracer routine in my application for solving this issue.
Thanks for your suggestions & specially for the link. I am working on how to use the "Trace" class for tracking the application, as I had not used it before.
Thanks a lot.
- Editado Nouman Umar sexta-feira, 13 de abril de 2012 10:49
sexta-feira, 13 de abril de 2012 13:27
If you need more information in regards to Armin suggestion and a working example which logs unhandled exceptions to an xml based file take a look here
- Sugerido como Resposta Reed KimbleMicrosoft Community Contributor, Moderator sexta-feira, 13 de abril de 2012 20:36
sexta-feira, 13 de abril de 2012 20:03Thanks kevin for your help.
segunda-feira, 16 de abril de 2012 12:49
I have solved the problem of tracking the method name which caused unhandled exception to occur, using StackTrace & StackFrame class.
here is the code.
Dim st As StackTrace = New StackTrace(e.Exception, True) Dim sf As StackFrame = st.GetFrame(1) Dim method As System.Reflection.MethodBase method = sf.GetMethod() MessageBox.Show("Error occurred: " & e.Exception.Message & Environment.NewLine & "in Method: " & method.Name)Thanks everyone for your help!
segunda-feira, 16 de abril de 2012 13:20You don't need to create a new stacktrace as e.exception already has a StackTrace property.
terça-feira, 17 de abril de 2012 04:41Thanks Armin, but in that case how could I call the GetMethod() function using StackTrace property?
terça-feira, 17 de abril de 2012 10:22The Stacktrace String also contains the name of that method (as well as all calling methods). But of course, you can also retrieve additional information about the method your way if the name is not enough.
sexta-feira, 20 de abril de 2012 11:32
There is another way for tracing the method name which caused exception using following code, which is similar as mentioned by Armin.
' For getting method name with it's arguments & return type Dim MethodName As String = e.Exception.TargetSite.ToString() ' For getting the class/form name of Method Dim CLassName As String = e.Exception.TargetSite.DeclaringType.FullName
- Marcado como Resposta Nouman Umar sexta-feira, 20 de abril de 2012 11:33