By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

Global Exception Handler

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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" <cl*****@mtgc.net> wrote in message
news:bu************@ID-198343.news.uni-berlin.de...
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

Nov 20 '05 #2

P: n/a
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]" <Ja************@msn.com> ha scritto nel
messaggio news:ux**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #3

P: n/a
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" <cl*****@mtgc.net> wrote in message
news:bu************@ID-198343.news.uni-berlin.de...
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]" <Ja************@msn.com> ha scritto nel
messaggio news:ux**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #4

P: n/a
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" <cl*****@mtgc.net> wrote in message
news:bu************@ID-198343.news.uni-berlin.de... 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]" <Ja************@msn.com> ha scritto nel
messaggio news:ux**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #5

P: n/a
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]" <Ja************@msn.com> ha scritto nel
messaggio news:OT**************@tk2msftngp13.phx.gbl...
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" <cl*****@mtgc.net> wrote in message
news:bu************@ID-198343.news.uni-berlin.de...
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]" <Ja************@msn.com> ha scritto nel
messaggio news:ux**************@TK2MSFTNGP10.phx.gbl...
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



Nov 20 '05 #6

P: n/a
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" <cl***********@mtgcNOSPAM.net> wrote in message
news:7q*********************@news3.tin.it... 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]" <Ja************@msn.com> ha scritto nel
messaggio news:OT**************@tk2msftngp13.phx.gbl...
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" <cl*****@mtgc.net> wrote in message
news:bu************@ID-198343.news.uni-berlin.de...
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]" <Ja************@msn.com> ha scritto

nel messaggio news:ux**************@TK2MSFTNGP10.phx.gbl...
> 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
>



Nov 20 '05 #7

P: n/a
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" <cl***********@mtgcNOSPAM.net> wrote in message
news:7q*********************@news3.tin.it...
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]" <Ja************@msn.com> ha scritto nel
messaggio news:OT**************@tk2msftngp13.phx.gbl...
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" <cl*****@mtgc.net> wrote in message
news:bu************@ID-198343.news.uni-berlin.de...
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]" <Ja************@msn.com> ha scritto

nel messaggio news:ux**************@TK2MSFTNGP10.phx.gbl...
> 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
>



Nov 20 '05 #8

P: n/a
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]" <Ja************@msn.com> ha scritto nel
messaggio news:uE**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #9

P: n/a
Claudio,
That is the way I am recommending.

Hope this helps
Jay
"Claudio Di Flumeri" <cl*****@mtgc.net> wrote in message
news:bv************@ID-198343.news.uni-berlin.de...
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]" <Ja************@msn.com> ha scritto nel
messaggio news:uE**************@TK2MSFTNGP09.phx.gbl...
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


Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.