How to track the routine which caused unhandled exception event (Application Events) to occur
-
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.
Thanks.
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.
EDIT:
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
Armin
- 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
Nouman,
If you have used this kind of code (and that is where it often appears) then the answer is no.
Try
DoSomething
Catch
'DoNothing
End TryThen handle first all the catches.
Success
Cor- 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.
Thanks again.
-
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
Thanks Cor!
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
Nouman,
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.
http://msdn.microsoft.com/en-us/library/ms172752.aspx
There is no debug step back available (there is a trick to do that but not when an exception is thrown).
Success
Cor -
quinta-feira, 12 de abril de 2012 19:45
What's wrong with a breakpoint?
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
sexta-feira, 13 de abril de 2012 10:48
Cor,
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
http://code.msdn.microsoft.com/Dealing-with-unhandled-9b933818
KSG
- 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!
- Marcado como Resposta Nouman Umar segunda-feira, 16 de abril de 2012 12:49
- Editado Nouman Umar segunda-feira, 16 de abril de 2012 12:51
-
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.
Armin
-
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.
Armin
-
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
Thanks.- Marcado como Resposta Nouman Umar sexta-feira, 20 de abril de 2012 11:33

