Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global Exception Handler

2 views
Skip to first unread message

Claudio Di Flumeri

unread,
Jan 23, 2004, 3:39:16 AM1/23/04
to
Hello all,

I've added a global exception handler to my application in this way:

Sub Main()
AddHandler Application.ThreadException, AddressOf ThreadException
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
UnhandledException

Application.Run(Main)
End Sub

At one point in my application, I use the
System.Diagnostics.Process.GetProcesses() method, and it fires a
System.InvalidOperationException error (I know why, it's related to the
Performance Counter Disabled in the Registry, so this is not an issue). But
why the exception is not catched by the two exception handler I've declared
in the Sub Main?

Thanks


Jay B. Harlow [MVP - Outlook]

unread,
Jan 23, 2004, 9:05:25 AM1/23/04
to
Claudio,
What do you mean specifically by "not caught"?

- Is your app failing with that exception?
- Is it that the global handlers are not entered?
- Do you have a Try/Catch around the block of code where you have
Process.GetProcesses?
- is Process.GetProcesses in its own thread?
- is Process.GetProcesses in its own appdomain?

Can you give more details on where & how Process.GetProcesses is being
called?

Hope this helps
Jay

"Claudio Di Flumeri" <cla...@mtgc.net> wrote in message
news:buqmjl$kjqne$1...@ID-198343.news.uni-berlin.de...

Claudio Di Flumeri

unread,
Jan 23, 2004, 9:40:56 AM1/23/04
to
Jay,

Yes my application fails with that exception, and the global handlers are
not entered. I don't have a Try - Catch around that block of code, just
because I want the exception being intercepted by one of the 2 global
handlers (but it doesn't happen). The Process.GetProcesses it's not on his
own thread, because I've entered it in Sub Main just after the Global
Handlers. My code is something like this:


Private Sub Application_ThreadException(ByVal sender As Object, ByVal e As
System.Threading.ThreadExceptionEventArgs)
'My routine to show an error message box
MessaggioErrore(e.Exception, e.Exception.TargetSite)
End Sub

Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e
As UnhandledExceptionEventArgs)
'My routine to show an error message box
MessaggioErrore(e.ExceptionObject, CType(e.ExceptionObject,
Exception).TargetSite)
End Sub

Sub Main()
AddHandler Application.ThreadException, AddressOf

Application_ThreadException
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
Application_UnhandledException

Dim Aux As Integer = System.Diagnostics.Process.GetProcesses().Length

Application.Run(Main)
End Sub

I don't understand why this exception is not caught!

"Jay B. Harlow [MVP - Outlook]" <Jay_Har...@msn.com> ha scritto nel
messaggio news:ux6c7nb4...@TK2MSFTNGP10.phx.gbl...

Trev Hunter

unread,
Jan 23, 2004, 1:49:09 PM1/23/04
to
It's probably because the exception is happening before the Application is
actually started.

Note the difference beteen the "Application" object (which starts with the
call to Application.Run), and your process (which starts at sub main). The
application object serves as a host for Windows Forms applications - it
provides message loop functions etc.

Because the exception is raised outside the context of the application
object, it cannot be caught by its error handlers. Only exceptions that
happen as part of the message loop inside the Application.Run method can be
caught in this way.

Try moving the line that raises the exception to the Constructor of your
main form and it should work.


HTH,

Trev.


"Claudio Di Flumeri" <cla...@mtgc.net> wrote in message

news:burbj2$l2bel$1...@ID-198343.news.uni-berlin.de...

Jay B. Harlow [MVP - Outlook]

unread,
Jan 23, 2004, 6:12:38 PM1/23/04
to
Claudio,

> Yes my application fails with that exception, and the global handlers are
> not entered.
Have you verified this with breakpoints?


As Trev stated: Application_ThreadException will not catch the exception as
Application.ThreadException only handles unhandled exceptions thrown within
Application.Run, in other words only uncaught exceptions from your Windows
Forms Events.

However AppDomain_UnhandledException will handle it, as your exception
caused Sub Main to exit with an unhandled exception, of course once Sub Main
exits your program is finished.

You should be seeing the "MessaggioErrore" routine to be called, then your
program to exit! Are you seeing something else?

Changing:

> Dim Aux As Integer = System.Diagnostics.Process.GetProcesses().Length

To:
Throw New Exception("This is Test")

VS.NET 2003 will stop on the Application_UnhandledException, after setting
breakpoints in both exception handlers. I did not verify in VS.NET 2002,
however I would expect the same behavior!

Hope this helps
Jay

"Claudio Di Flumeri" <cla...@mtgc.net> wrote in message

news:burbj2$l2bel$1...@ID-198343.news.uni-berlin.de...

Claudio Di Flumeri

unread,
Jan 24, 2004, 8:28:35 AM1/24/04
to
Thanks for your replies,

Actually what is happen is the following:
When I am in debug mode and I launch my application from the IDE of Visual
Studio, the program stops at the instruction

Dim Aux As Integer = System.Diagnostics.Process.GetProcesses().Length

not entering in my exception handlers.
If I compile the application in Release mode and then I launch the .EXE file
(so not from the IDE), it gives me a normal error (A MsgBox saying that no
JIT debugger has been found) and AFTER that it launches my MessaggioErrore
window, likely the one declared in the AppDomain_UnhandledException
subroutine. I'd like that the program would launch immediately my window and
not the one with the "debugger not found error" before, but it's not a big
problem (because I understand that every exception will be caught after the
Application.Run), just curious!

Claudio

"Jay B. Harlow [MVP - Outlook]" <Jay_Har...@msn.com> ha scritto nel

messaggio news:OTS0uZg4...@tk2msftngp13.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Jan 24, 2004, 10:46:07 AM1/24/04
to
Claudio,

> When I am in debug mode and I launch my application from the IDE of Visual
> Studio, the program stops at the instruction

You can control having it stop on the line in error via 'Debug -
Exceptions - Common Language Runtime Exceptions - If the exception is not
handled - break into the debugger'.

If you press Continue, the AppDomain_UnhandledException will be found.

> If I compile the application in Release mode and then I launch the .EXE
file
> (so not from the IDE), it gives me a normal error (A MsgBox saying that no
> JIT debugger has been found) and AFTER that it launches my MessaggioErrore

Do you get the JIT debugger on just your development machine or all
machines? I only have immediate access to development machines (machines
that have VS.NET on them) and I get the normal JIT debugger dialog listing
VS.NET when running the program outside the IDE.

> I'd like that the program would launch immediately my window and
> not the one with the "debugger not found error" before, but it's not a big
> problem (because I understand that every exception will be caught after
the
> Application.Run), just curious!

I would simply wrap Sub Main or more specifically Process.GetProcesses in a
Try Catch. ;-)

I remember there being an option to control the JIT debugger window, however
I do not have a reference handy as to whether its in the app.config or
windows itself. I will try to find it later & report back.

Hope this helps
Jay

"Claudio Di Flumeri" <claudi...@mtgcNOSPAM.net> wrote in message
news:7quQb.138692$VW.55...@news3.tin.it...

Jay B. Harlow [MVP - Outlook]

unread,
Jan 24, 2004, 12:06:05 PM1/24/04
to
Claudio,
I tested my version under Windows 2003 Server (without VS.NET installed).

I get a dialog that states "Common Language Runtime Debugger services" with
options to click OK to terminate & Cancel to debug.

I will see if I can find out more info...

However as I pointed out in my other post, the easy way around the other
message is to put a try/catch in your Sub Main.

Hope this helps
Jay

"Claudio Di Flumeri" <claudi...@mtgcNOSPAM.net> wrote in message
news:7quQb.138692$VW.55...@news3.tin.it...

Claudio Di Flumeri

unread,
Jan 26, 2004, 4:03:28 AM1/26/04
to
Jay,

Thanks for your replies! Till now I haven't found a way to "jump" the dialog
with the debugger error (launching the .EXE file directly), anyway for what
I've understood the better way to act is doing something like this:

Sub Main()

AddHandler Application.ThreadException, AddressOf
Application_ThreadException
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf
Application_UnhandledException

Try
<instructions executed before the Application.Run>
Catch ex as Exception
MessaggioErrore.....
End Try

Application.Run(Main)
End Sub

After the Application.Run, exceptions' message errors will be managed with
the global handlers (thanks to the two initial AddHandler's). Could be
right?
Claudio


"Jay B. Harlow [MVP - Outlook]" <Jay_Har...@msn.com> ha scritto nel

messaggio news:uE9wgxp4...@TK2MSFTNGP09.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Jan 26, 2004, 2:13:05 PM1/26/04
to
Claudio,
That is the way I am recommending.

Hope this helps
Jay


"Claudio Di Flumeri" <cla...@mtgc.net> wrote in message

news:bv2l4u$nj832$1...@ID-198343.news.uni-berlin.de...

0 new messages