473,836 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UnhandledExcept ion in Windows Service

I can't seem to get a custom UnhandledExcept ion handler working for a
Windows Service I'm writing in VB.NET. I've read the MSDN and tried
various different placements of the AddHandler call, to no avail.

Here's the code I'm using, all of which is in the main service class:
Protected Overrides Sub OnStart(ByVal args() As String)

AddHandler AppDomain.Curre ntDomain.Unhand ledException, _
AddressOf UnhandledExcept ionHandler
Throw New Exception

End Sub

Sub UnhandledExcept ionHandler(ByVa l sender As Object, _
ByVal e As System.Unhandle dExceptionEvent Args)

Me.EventLog.Wri teEntry("This entry will never be written, " & _
"because the programmer doesn't know what he's doing.")

End Sub

Any help would be appreciated, so if you want to be appreciated by an
idiot, please help.

Nov 21 '05 #1
4 5623
Groundskeeper,
AppDomain.Unhan dledException only handles unhandled exceptions.

The OnStart routine has Try/Catch around it, hence the
UnhandledExcept ionHandler is not called.

You can tell that OnStart has a Try/Catch around it, because if OnStart
throws an exception the service remains at in the Stop state, with an error
that is automatically logged.

Note its common to directly or indirectly to use Asynchronous Delegates in a
Windows Service, AppDomain.Unhan dledException is not used for Asynchronous
Delegates either, you need to be certain to use end EndInvoke with
Asynchronous & catch the exception there, with possible logging. As far as I
can tell the System.Timers.T imer.Elapsed event is called via an Asynchronous
Delegate, which means you should wrap the Elapsed event handler in a
Try/Catch so as not to loose the Exception.

As far as I can tell ThreadPool.Queu eUserWorkItem uses
AppDomain.Unhan dledException so you should be safe there. This goes for
System.Threadin g.Timer also.

Hope this helps
Jay

"Groundskee per" <jf******@austi n.rr.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I can't seem to get a custom UnhandledExcept ion handler working for a
Windows Service I'm writing in VB.NET. I've read the MSDN and tried
various different placements of the AddHandler call, to no avail.

Here's the code I'm using, all of which is in the main service class:
Protected Overrides Sub OnStart(ByVal args() As String)

AddHandler AppDomain.Curre ntDomain.Unhand ledException, _
AddressOf UnhandledExcept ionHandler
Throw New Exception

End Sub

Sub UnhandledExcept ionHandler(ByVa l sender As Object, _
ByVal e As System.Unhandle dExceptionEvent Args)

Me.EventLog.Wri teEntry("This entry will never be written, " & _
"because the programmer doesn't know what he's doing.")

End Sub

Any help would be appreciated, so if you want to be appreciated by an
idiot, please help.

Nov 21 '05 #2
That helps, thanks.

The service in question runs a timer (System.Timers. Timer) to watch for
files to process. (Earlier versions had used the filesystemwatch er, but
ran in to problems with it, but that's another story.) From what you
say, it sounds like simply wrapping the code in the timer's Elapsed
event and the OnStart and OnStop service events in Try/Catch blocks
should catch just about anything I can hope to catch.

Out of curiosity, is it only through trial and error that one can
discover which procedures can be expected to raise errors to a global
handler for unhandled exceptions? Put another way, when is adding a
handler for unhandled exceptions useful/needed in a Windows Service?

Jay B. Harlow [MVP - Outlook] wrote:
Groundskeeper,
AppDomain.Unhan dledException only handles unhandled exceptions.

The OnStart routine has Try/Catch around it, hence the
UnhandledExcept ionHandler is not called.

You can tell that OnStart has a Try/Catch around it, because if OnStart throws an exception the service remains at in the Stop state, with an error that is automatically logged.

Note its common to directly or indirectly to use Asynchronous Delegates in a Windows Service, AppDomain.Unhan dledException is not used for Asynchronous Delegates either, you need to be certain to use end EndInvoke with
Asynchronous & catch the exception there, with possible logging. As far as I can tell the System.Timers.T imer.Elapsed event is called via an Asynchronous Delegate, which means you should wrap the Elapsed event handler in a
Try/Catch so as not to loose the Exception.

As far as I can tell ThreadPool.Queu eUserWorkItem uses
AppDomain.Unhan dledException so you should be safe there. This goes for System.Threadin g.Timer also.

Hope this helps
Jay

"Groundskee per" <jf******@austi n.rr.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I can't seem to get a custom UnhandledExcept ion handler working for a Windows Service I'm writing in VB.NET. I've read the MSDN and tried
various different placements of the AddHandler call, to no avail.

Here's the code I'm using, all of which is in the main service class:

Protected Overrides Sub OnStart(ByVal args() As String)

AddHandler AppDomain.Curre ntDomain.Unhand ledException, _
AddressOf UnhandledExcept ionHandler
Throw New Exception

End Sub

Sub UnhandledExcept ionHandler(ByVa l sender As Object, _
ByVal e As System.Unhandle dExceptionEvent Args)

Me.EventLog.Wri teEntry("This entry will never be written, " & _
"because the programmer doesn't know what he's doing.")

End Sub

Any help would be appreciated, so if you want to be appreciated by an idiot, please help.


Nov 21 '05 #3
Groundskeeper,
say, it sounds like simply wrapping the code in the timer's Elapsed
event and the OnStart and OnStop service events in Try/Catch blocks I am saying wrap the Timer's Elapsed event in a Try/Catch block.

The OnStart & OnStop are already wrapped in Try/Catch blocks! (inside of the
ServiceBase class)
Out of curiosity, is it only through trial and error that one can
discover which procedures can be expected to raise errors to a global Basically that is it & reading the documentation
(http://msdn.microsoft.com/library).
Put another way, when is adding a
handler for unhandled exceptions useful/needed in a Windows Service? When you are using ThreadPool.Queu eUserWorkItem & System.Threadin g.Timer.

My observations are based on .NET 1.1 (VS.NET 2003) things may very well
change in .NET 2.0 (VS.NET 2005 aka Whidbey, due out later in 2005) & .NET
1.0.

Hope this helps
Jay

"Groundskee per" <jf******@austi n.rr.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com... That helps, thanks.

The service in question runs a timer (System.Timers. Timer) to watch for
files to process. (Earlier versions had used the filesystemwatch er, but
ran in to problems with it, but that's another story.) From what you
say, it sounds like simply wrapping the code in the timer's Elapsed
event and the OnStart and OnStop service events in Try/Catch blocks
should catch just about anything I can hope to catch.

Out of curiosity, is it only through trial and error that one can
discover which procedures can be expected to raise errors to a global
handler for unhandled exceptions? Put another way, when is adding a
handler for unhandled exceptions useful/needed in a Windows Service?

Jay B. Harlow [MVP - Outlook] wrote:
Groundskeeper,
AppDomain.Unhan dledException only handles unhandled exceptions.

The OnStart routine has Try/Catch around it, hence the
UnhandledExcept ionHandler is not called.

You can tell that OnStart has a Try/Catch around it, because if

OnStart
throws an exception the service remains at in the Stop state, with an

error
that is automatically logged.

Note its common to directly or indirectly to use Asynchronous

Delegates in a
Windows Service, AppDomain.Unhan dledException is not used for

Asynchronous
Delegates either, you need to be certain to use end EndInvoke with
Asynchronous & catch the exception there, with possible logging. As

far as I
can tell the System.Timers.T imer.Elapsed event is called via an

Asynchronous
Delegate, which means you should wrap the Elapsed event handler in a
Try/Catch so as not to loose the Exception.

As far as I can tell ThreadPool.Queu eUserWorkItem uses
AppDomain.Unhan dledException so you should be safe there. This goes

for
System.Threadin g.Timer also.

Hope this helps
Jay

"Groundskee per" <jf******@austi n.rr.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
>I can't seem to get a custom UnhandledExcept ion handler working for a > Windows Service I'm writing in VB.NET. I've read the MSDN and tried
> various different placements of the AddHandler call, to no avail.
>
> Here's the code I'm using, all of which is in the main service class: >
>
> Protected Overrides Sub OnStart(ByVal args() As String)
>
> AddHandler AppDomain.Curre ntDomain.Unhand ledException, _
> AddressOf UnhandledExcept ionHandler
> Throw New Exception
>
> End Sub
>
> Sub UnhandledExcept ionHandler(ByVa l sender As Object, _
> ByVal e As System.Unhandle dExceptionEvent Args)
>
> Me.EventLog.Wri teEntry("This entry will never be written, " & _
> "because the programmer doesn't know what he's doing.")
>
> End Sub
>
>
>
> Any help would be appreciated, so if you want to be appreciated by an > idiot, please help.
>

Nov 21 '05 #4
I really appreciate your replies. Sorry I didn't reply more recently;
I'm new to Google groups and had expected an email notifying me of any
activity on this thread.

What I meant by suggesting that OnStart and OnStop should have
Try/Catch blocks inside them is that this way, I can catch the errors
in my code and handle them myself, rather than relying on the default
handling in ServiceBase.

Once again, this idiot appreciates your help.

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7557
by: Mike Strieder | last post by:
i want to catch all execeptions with my own handler, but the problem is, that the JIT-Debugger always start! i put also the <system.windows.forms jitDebugging="false" /> in the machine.config thx for help Mike
4
2635
by: Thom Little | last post by:
I have a simple C# Winforms application that has an UnhandledException handler that writes to the log. This works fine if I throw a new exception (like"bogus") but it does not gain control on a Zero Divide exception. Is the UnhandledException handler supposed to receive control on all exceptions or is the UnhandledException hander supposed to receive control on all exceptions after all other exception handlers have "had a chance" to...
1
2551
by: Kimmo Laine | last post by:
Hi, does the AppDomain.UnhandledException also work when you use it in Windows Service - i was unable to get the handler called when i generated an exception. thx Kimmo Laine
5
1952
by: John Richardson | last post by:
Quick question about the UnhandledException event and associated Handler. I just implemented this handler for the first time, and am surprised that it this event is being raised for an exception that I have handled. I have the following: private void Load() { try {
2
2342
by: guy | last post by:
In my application I have an event handler to catch unhandled exceptions, as per the MSDN documentation, however when an unhandled exception is thrown I get the Dialogue first, then the event handler catches the exception. How do I supress the dialogue? guy
2
6545
by: Chris Dunaway | last post by:
I am attempting to use the AppDomain.UnhandledException event in a Windows Forms app and also in a Windows Service. But the event doesn't seem to be called. In a Windows Forms app, the event IS called but only if I run the app through the IDE. If run standalone (release or debug build, it doesn't matter), the event handler is never called. In the Windows Forms app, I changed it to use the Application.ThreadException event and that...
0
1103
by: Gregor Horvath | last post by:
Hi, in a windows server python application I receive once a week suddenly and not reproducible the following error. UnhandledException.rpt file: <file> //=========================================================================================================== Exception code: C0000090 FLT_INVALID_OPERATION Program: C:\Python24\python.exe Date: 6/ 6/2006 14:36:13 (MM/DD/YYYY HH:MM:SS)
7
1698
by: Spam Catcher | last post by:
Hi all, When I'm debugging my application in VS.NET 2005, the debugger intercepts all unhandled exceptions and does not break into ApplicationEvents.UnhandledException. How do I debug ApplicationEvents.UnhandledException? I have some elaborate code in there which I would like to test. ApplicationEvents.UnhandledException only seems to be executed if I do not
1
1665
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, is it possible use AppDomain.Currentdomain. UnhandledException event en ASP.NET 2.0 ?? I have this code: protected override void OnInit(EventArgs e) { base.OnInit(e);
0
9671
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10600
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4459
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.