Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to catch all unhandled exceptions ?

How to catch all unhandled exceptions ?

Scheduled Pinned Locked Moved Visual Basic
helpcsharpvisual-studiowinformsdesign
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xx77abs
    wrote on last edited by
    #1

    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 :)

    X L 2 Replies Last reply
    0
    • X xx77abs

      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 :)

      X Offline
      X Offline
      xx77abs
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • X xx77abs

        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

        D Offline
        D Offline
        DaveAuld
        wrote on last edited by
        #3

        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

        X 1 Reply Last reply
        0
        • D DaveAuld

          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

          X Offline
          X Offline
          xx77abs
          wrote on last edited by
          #4

          I'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 ;)

          1 Reply Last reply
          0
          • X xx77abs

            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 :)

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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.

            X 1 Reply Last reply
            0
            • L Luc Pattyn

              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.

              X Offline
              X Offline
              xx77abs
              wrote on last edited by
              #6

              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

              L 1 Reply Last reply
              0
              • X xx77abs

                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

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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.

                X 1 Reply Last reply
                0
                • L Luc Pattyn

                  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.

                  X Offline
                  X Offline
                  xx77abs
                  wrote on last edited by
                  #8

                  I've tried to google that, but can't find anything ... Well, I'm just happy that my app now works :D It could be that this doesn't happen in .NET 2.0 ether :) Anyway, thanks for helping :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups