How to catch all unhandled exceptions ?
-
Hello ! I'm having a problem with catching all unhandled exceptions, without try ... catch block, so that if my program get's exception that I didn't count on, it could write it to file or send it to me over the net and close "nicely". So far I've been using this:
' Add the event handler for handling UI thread exceptions to the event. AddHandler Application.ThreadException, AddressOf ErrorReporting.ReportError ' Set the unhandled exception mode to force all Windows Forms errors to go through ' our handler. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) ' Add the event handler for handling non-UI thread exceptions to the event. AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf ErrorReporting.ReportError
and it was working perfectly ... Until few days ago :( Now it doesn't catch anything. Even when I'm running program from Visual Studio 2010, if unhandled exception occurs, my program just shuts off, and Visual Studio doesn't show me where exception occurred or what the exception was ... Please help me somehow, because this is very frustrating, and it's making programming impossible ... Thanks :)
-
Hello ! I'm having a problem with catching all unhandled exceptions, without try ... catch block, so that if my program get's exception that I didn't count on, it could write it to file or send it to me over the net and close "nicely". So far I've been using this:
' Add the event handler for handling UI thread exceptions to the event. AddHandler Application.ThreadException, AddressOf ErrorReporting.ReportError ' Set the unhandled exception mode to force all Windows Forms errors to go through ' our handler. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) ' Add the event handler for handling non-UI thread exceptions to the event. AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf ErrorReporting.ReportError
and it was working perfectly ... Until few days ago :( Now it doesn't catch anything. Even when I'm running program from Visual Studio 2010, if unhandled exception occurs, my program just shuts off, and Visual Studio doesn't show me where exception occurred or what the exception was ... Please help me somehow, because this is very frustrating, and it's making programming impossible ... Thanks :)
After trying everything, I've created new project and started to copy parts of my project in which error handling is broken ... And I've found out that my application catches all exceptions until this line: req = CType(HttpWebRequest.Create(New Uri("http://test.com")), HttpWebRequest) So what's the problem ? I don't think that I've did any thing wrong. Another interesting thing is that if I change my project to .NET framework 4.0 (currently it's 3.5), it works OK ... So is this kinda of bug in 3.5 or what ? Thanks :D
-
After trying everything, I've created new project and started to copy parts of my project in which error handling is broken ... And I've found out that my application catches all exceptions until this line: req = CType(HttpWebRequest.Create(New Uri("http://test.com")), HttpWebRequest) So what's the problem ? I don't think that I've did any thing wrong. Another interesting thing is that if I change my project to .NET framework 4.0 (currently it's 3.5), it works OK ... So is this kinda of bug in 3.5 or what ? Thanks :D
Read this line from the docs; Do not use the HttpWebRequest constructor. Use the WebRequest.Create method to initialize new HttpWebRequest objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://, Create returns an HttpWebRequest object. Based on this, you should be doing; req = WebRequest.Create(), no need for any ctype.
Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.com -
Read this line from the docs; Do not use the HttpWebRequest constructor. Use the WebRequest.Create method to initialize new HttpWebRequest objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://, Create returns an HttpWebRequest object. Based on this, you should be doing; req = WebRequest.Create(), no need for any ctype.
Dave Don't forget to rate messages!
Find Me On: Web|Facebook|Twitter|LinkedIn
Waving? dave.m.auld[at]googlewave.comI've tried to replace
req = CType(HttpWebRequest.Create(New Uri("http://test.com")), HttpWebRequest)
with
req = WebRequest.Create(New Uri("http://test.com"))
but the result is the same - program can't catch any error after that line ... As for using CType, I must use it because I've turned strict on in my project ;)
-
Hello ! I'm having a problem with catching all unhandled exceptions, without try ... catch block, so that if my program get's exception that I didn't count on, it could write it to file or send it to me over the net and close "nicely". So far I've been using this:
' Add the event handler for handling UI thread exceptions to the event. AddHandler Application.ThreadException, AddressOf ErrorReporting.ReportError ' Set the unhandled exception mode to force all Windows Forms errors to go through ' our handler. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) ' Add the event handler for handling non-UI thread exceptions to the event. AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf ErrorReporting.ReportError
and it was working perfectly ... Until few days ago :( Now it doesn't catch anything. Even when I'm running program from Visual Studio 2010, if unhandled exception occurs, my program just shuts off, and Visual Studio doesn't show me where exception occurred or what the exception was ... Please help me somehow, because this is very frustrating, and it's making programming impossible ... Thanks :)
The event handlers for Application.ThreadException and AppDomain.UnhandledException have different parameters, so I doubt your code is correct as it uses a shared handler method. I suggest you check the documentation, fix the problem, and start your source files with
Option Strict On
. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
The event handlers for Application.ThreadException and AppDomain.UnhandledException have different parameters, so I doubt your code is correct as it uses a shared handler method. I suggest you check the documentation, fix the problem, and start your source files with
Option Strict On
. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
In my last post I've told daveauld that I've turned strict on in my whole project :) As for Application.ThreadException and Appdomain.UnhandledException, I know that they have different parameters, that's why I have overloaded ReportError sub, so that they booth can use it ;) Like this:
Public Sub ReportError(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs) Try MsgBox("Unhandled error has occured !!" & vbCrLf & vbCrLf & e.Exception.Message & vbCrLf & \_ErrorTitle & " will now close.", \_ CType(MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, MsgBoxStyle), \_ErrorTitle) WriteError(sender, e.Exception, "Yes") If Not System.Diagnostics.Debugger.IsAttached Then Application.Exit() End If Catch End Try End Sub Public Sub ReportError(ByVal sender As Object, ByVal e As System.UnhandledExceptionEventArgs) Try MsgBox("Unhandled error has occured !!" & vbCrLf & vbCrLf & CType(e.ExceptionObject, Exception).Message & vbCrLf & \_ErrorTitle & " will now close.", \_ CType(MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, MsgBoxStyle), \_ErrorTitle) WriteError(sender, CType(e.ExceptionObject, Exception), "No") If Not System.Diagnostics.Debugger.IsAttached Then Application.Exit() End If Catch End Try End Sub
And I've told you, this is not working only in .NET Framework 3.5 (after creating HTTPWebRequest object) ;) Right now I've switched my project to .NET Framework 4.0, and the same code is working perfectly
-
In my last post I've told daveauld that I've turned strict on in my whole project :) As for Application.ThreadException and Appdomain.UnhandledException, I know that they have different parameters, that's why I have overloaded ReportError sub, so that they booth can use it ;) Like this:
Public Sub ReportError(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs) Try MsgBox("Unhandled error has occured !!" & vbCrLf & vbCrLf & e.Exception.Message & vbCrLf & \_ErrorTitle & " will now close.", \_ CType(MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, MsgBoxStyle), \_ErrorTitle) WriteError(sender, e.Exception, "Yes") If Not System.Diagnostics.Debugger.IsAttached Then Application.Exit() End If Catch End Try End Sub Public Sub ReportError(ByVal sender As Object, ByVal e As System.UnhandledExceptionEventArgs) Try MsgBox("Unhandled error has occured !!" & vbCrLf & vbCrLf & CType(e.ExceptionObject, Exception).Message & vbCrLf & \_ErrorTitle & " will now close.", \_ CType(MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, MsgBoxStyle), \_ErrorTitle) WriteError(sender, CType(e.ExceptionObject, Exception), "No") If Not System.Diagnostics.Debugger.IsAttached Then Application.Exit() End If Catch End Try End Sub
And I've told you, this is not working only in .NET Framework 3.5 (after creating HTTPWebRequest object) ;) Right now I've switched my project to .NET Framework 4.0, and the same code is working perfectly
Seems you know what you are doing then. I use HttpWebRequest a lot and never had such issues; I mostly use C# and .NET 2.0 though. Maybe Google can tell you about some bug that got solved in 4.0? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Seems you know what you are doing then. I use HttpWebRequest a lot and never had such issues; I mostly use C# and .NET 2.0 though. Maybe Google can tell you about some bug that got solved in 4.0? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.