Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Do something on an uncaught exception.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 08 Aug 2007 06:50 GMT
Hi all,

I have a small helper service that is peppered with try-catches for all the
exceptional cases I could think of.
It industriously logs every error it comes across for debugging later on.

Wonderful; however on the odd time it blows a gasket the solution is in
99.9% the same .. restart the service.
Then go away and track down the source of the bug fix-test-deploy.
usually a 5 min fix .. but often a call at 3am ...

Is there a mechanism for catching these "uncaught" exceptions in the
application so that I could then launch another instance of the service and
gracefully close down the 'afflicted' app.

regards Bob

p.s. .. I'm sure explorer.exe does this .. it seems to hang and restart all
the time.
Michael C - 08 Aug 2007 07:43 GMT
> Hi all,
>
[quoted text clipped - 10 lines]
> application so that I could then launch another instance of the service
> and gracefully close down the 'afflicted' app.

Application.ThreadException += new blah....

I'm not sure if you need to do this for each thread you create so it would
be worth testing here. With a service though don't you have a single point
that calls all code so can trap everything there? This is what I have and
don't need to restart the service.

Michael
AlanT - 08 Aug 2007 09:27 GMT
>>Is there a mechanism for catching these "uncaught" exceptions in the
>>application so that I could then launch another instance of the service and
>>gracefully close down the 'afflicted' app.

>Application.ThreadException += new blah....

If that does not catch everything you might also try the

   AppDomain.CurrentDomain.UnhandledException += ....

hth,
Alan.
GlennDoten - 09 Aug 2007 14:07 GMT
> Application.ThreadException += new blah....
>
> I'm not sure if you need to do this for each thread you create so it would
> be worth testing here. With a service though don't you have a single point
> that calls all code so can trap everything there? This is what I have and
> don't need to restart the service.

Application.ThreadException is documented as handling any exception that
may occur in the application's UI thread, so there's only one thread
this event applies to. Correct?

I'm wondering if a service application even has a UI thread like a
Windows Forms application does. If not, is there any point in calling
Application.ThreadException in a service application?

Signature

-glenn-

Michael C - 13 Aug 2007 00:18 GMT
> Application.ThreadException is documented as handling any exception that
> may occur in the application's UI thread, so there's only one thread this
[quoted text clipped - 3 lines]
> Forms application does. If not, is there any point in calling
> Application.ThreadException in a service application?

In a service most code will start from a single point which will probably be
a timer, so it's easy to catch any exception there.
Mike Blake-Knox - 13 Aug 2007 17:50 GMT
> In a service most code will start from a single point which will probably be 
> a timer, so it's easy to catch any exception there.

Except for thread pool threads and asynchronous callback threads. For instance,
an exception thrown in a handler for completion of a socket receive needs to be
handled in a try/catch handler (or by the handler for
AppDomain.CurrentDomain.UnhandledException) as Allan suggested.

Mike
Michael C - 14 Aug 2007 00:48 GMT
> Except for thread pool threads and asynchronous callback threads. For
> instance,
> an exception thrown in a handler for completion of a socket receive needs
> to be
> handled in a try/catch handler (or by the handler for
> AppDomain.CurrentDomain.UnhandledException) as Allan suggested.

Excellent suggestion. This could be a problem in my service, I noticed some
exceptions not getting handled when I was developing even though I thought
everything should be caught. I think I probably eliminated them just by
fixing whatever was causing the exception but it would definately be worth
me revisiting it. Thanks for the tip.

Michael
ron willoughby - 08 Aug 2007 23:25 GMT
I am assuming that you are in .net. If so then above the page level which you seem to be catching there are the application level of global.aspx and also the last level below the server of web.config. Both have error traps that you can use. However, there are some errors that are a the server level that never get to the application and those are handled by the server. Look at Application_Error in global.aspx; and customerrors in the web.config.

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Bob - 09 Aug 2007 07:56 GMT
Thanks for this .. I'll give it a shot ...

regards Bob

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.