4

I have application with this code:

Module Startup
  <STAThread()> _
  Public Sub Main()
    Try
      Application.EnableVisualStyles()
      Application.SetCompatibleTextRenderingDefault(False)

      InitApp()

      Dim login As New LoginForm()
      Dim main As New MainForm()

      Application.Run(login)

      If login.DialogResult = DialogResult.OK Then
      ActUser = login.LoggedUser
      main.ShowDialog()
      End If

      DisposeApp()

    Catch ex As Exception
      ErrMsg(ex, "Error!", ErrorLogger.ErrMsgType.CriticalError)
      End 
    End Try
  End Sub
End Module

in debug mode everithing is OK. But in release mode when somewhere in application exception occurs my global catch in Main method doesn`t catch exception.

What is the problem please?

EDIT: unhandled exception from application is WebException thrown after failed web service call.

How can I handle this types of exceptions?

user349671
  • 73
  • 1
  • 4

2 Answers2

9

This is normal. There's an exception handler built into the Application.Run() method. It is disabled when you debug in order to make debugging exceptions easier. Without a debugger, the exception handler will display the ThreadExceptionDialog.

Add this statement at the top of your Main method to disable this exception handler:

   Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException)
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Might be of use: just figured out today on my winforms app that any exception that is thrown BEFORE the Application.Run method won't get caught by the try...catch block. You HAVE to do this otherwise your app might crash even using try...catch blocks. – Anderson Matos Feb 09 '12 at 23:02
0

Your exception is probably occurring on a non-main thread. Try hooking the AppDomain.UnhandledException event and logging out your exception info there:

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx

Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
  • Yes this can be exception in another thread because it is WebException after failed web service call. Anyway your advice doesn`t work :( – user349671 May 29 '10 at 23:49
  • I have described the problem - an exception on another thread. That answers your question "What is the problem please?". Perhaps you should amend your question to ask what it is you want to do? – Tim Lloyd May 29 '10 at 23:57