"Global" exception handling

T

Tom Berger

Just a short question concerning exception handling....

All of my applications contain an event handling in the Main() like this
one:

[STAThread]
static void Main()
{
// declare global exeption handler
Application.ThreadException += new
ThreadExceptionEventHandler(GlobalExceptionHandler.OnThreadException);
...
// load main form and start application
...
}

By that I am showing a special exception form to the user (with features
like "send bug report to ..."). This is for cases where I forgot a try/catch
somewhere.


Now I have to write a UserControl for an application that is out of my
control so I do not have personal influence on that code.
My UserControl is just loaded by that application, user can do several
things with it and it stays loaded all the time until the application is
closed again.

So the only entry point I have for this control is just the constructor.

How can I achieve to have a "global" exception handling just for my own
UserControl code? With Application.ThreadException I would include also the
hosting application...

Thanks in advance!
Tom
 
M

Michael Nemtsev [MVP]

Hello Tom,

what are u handling now, is only exception on the application level, without
taking into account different appDomains
For this u need to add AppDomain.UnhandledException handlin'

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


TB> Just a short question concerning exception handling....
TB>
TB> All of my applications contain an event handling in the Main() like
TB> this one:
TB>
TB> [STAThread]
TB> static void Main()
TB> {
TB> // declare global exeption handler
TB> Application.ThreadException += new
TB> ThreadExceptionEventHandler(GlobalExceptionHandler.OnThreadException
TB> );
TB> ...
TB> // load main form and start application
TB> ...
TB> }
TB> By that I am showing a special exception form to the user (with
TB> features like "send bug report to ..."). This is for cases where I
TB> forgot a try/catch somewhere.
TB>
TB> Now I have to write a UserControl for an application that is out of
TB> my
TB> control so I do not have personal influence on that code.
TB> My UserControl is just loaded by that application, user can do
TB> several
TB> things with it and it stays loaded all the time until the
TB> application is
TB> closed again.
TB> So the only entry point I have for this control is just the
TB> constructor.
TB>
TB> How can I achieve to have a "global" exception handling just for my
TB> own UserControl code? With Application.ThreadException I would
TB> include also the hosting application...
TB>
TB> Thanks in advance!
TB> Tom
 
T

Tom Berger

Hi Michael,

I guess that was the right hint, thanks for the answer.

Unfortunately I have no experience and no idea working with AppDomain.

What I forgot to write down in my first posting was: My UserControl is part
of an own project and is hosted within the main application project.
I have no influence on the code of the host application so I can not force
the developers to create an own AppDomain to run my assembly in.
But can I do it for my own?
In my UserControl constructor can I create an AppDomain from the scratch and
can I assign my own current Assemby to that AppDomain and then handle
UnhandledExceptions of this AppDomain?
Is this the way you suggest?
If so, how can I create a new AppDomain and assign my assembly?

Thanks for your further help and sorry for my annoying newbie requests...

Tom
 
M

Michael Nemtsev [MVP]

Hello Tom,

try handle unhandled exeption in your mail app and this allow u to handle
exeption even in other domains

u can find detailed info there http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


TB> Hi Michael,
TB>
TB> I guess that was the right hint, thanks for the answer.
TB>
TB> Unfortunately I have no experience and no idea working with
TB> AppDomain.
TB>
TB> What I forgot to write down in my first posting was: My UserControl
TB> is part
TB> of an own project and is hosted within the main application
TB> project.
TB> I have no influence on the code of the host application so I can not
TB> force
TB> the developers to create an own AppDomain to run my assembly in.
TB> But can I do it for my own?
TB> In my UserControl constructor can I create an AppDomain from the
TB> scratch and
TB> can I assign my own current Assemby to that AppDomain and then
TB> handle
TB> UnhandledExceptions of this AppDomain?
TB> Is this the way you suggest?
TB> If so, how can I create a new AppDomain and assign my assembly?
TB> Thanks for your further help and sorry for my annoying newbie
TB> requests...
TB>
TB> Tom
TB>
TB> TB>
Hello Tom,

what are u handling now, is only exception on the application level,
without taking into account different appDomains
For this u need to add AppDomain.UnhandledException handlin'
---
WBR, Michael Nemtsev [.NET/C# MVP] :: blog:
http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high
and we
miss it, but that it is too low and we reach it" (c) Michelangelo
TB> Just a short question concerning exception handling....
TB> TB> All of my applications contain an event handling in the
Main()
like
TB> this one:
TB> TB> [STAThread]
TB> static void Main()
TB> {
TB> // declare global exeption handler
TB> Application.ThreadException += new
TB>
ThreadExceptionEventHandler(GlobalExceptionHandler.OnThreadException
TB> );
TB> ...
TB> // load main form and start application
TB> ...
TB> }
TB> By that I am showing a special exception form to the user (with
TB> features like "send bug report to ..."). This is for cases where
I
TB> forgot a try/catch somewhere.
TB> TB> Now I have to write a UserControl for an application that is
out
of
TB> my
TB> control so I do not have personal influence on that code.
TB> My UserControl is just loaded by that application, user can do
TB> several
TB> things with it and it stays loaded all the time until the
TB> application is
TB> closed again.
TB> So the only entry point I have for this control is just the
TB> constructor.
TB> TB> How can I achieve to have a "global" exception handling just
for
my
TB> own UserControl code? With Application.ThreadException I would
TB> include also the hosting application...
TB> TB> Thanks in advance!
TB> Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top