473,659 Members | 3,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling unhandled exception in .net service

Hello,

I am using following code to handle unhandled exceptions in Windows Forms.

Shared Sub Main()

Dim GlobalException Handler As New GlobalException Handler

AddHandler Application.Thr eadException, AddressOf
GlobalException Handler.OnThrea dException

Application.Run (New MainForm)

End Sub

Exception Class:

Public Class GlobalException Handler

'Handle the exception event

Public Sub OnThreadExcepti on(ByVal sender As Object, ByVal t As
Threading.Threa dExceptionEvent Args)

'log exception

Helper.LogExcep tion(t.Exceptio n, "Unhandled exception.")

End Class

What code can I use to handle unhandled exception when developing a Windows
Service?

Thanks,

Pen D.
Jun 25 '07 #1
10 2463
"Penelope Dramas" <penelopeDOTdra masATpro-transportDOTcom wrote in message
news:e%******** ********@TK2MSF TNGP02.phx.gbl. ..
Hello,

I am using following code to handle unhandled exceptions in Windows Forms.

Shared Sub Main()

Dim GlobalException Handler As New GlobalException Handler

AddHandler Application.Thr eadException, AddressOf
GlobalException Handler.OnThrea dException

Application.Run (New MainForm)

End Sub

Exception Class:

Public Class GlobalException Handler

'Handle the exception event

Public Sub OnThreadExcepti on(ByVal sender As Object, ByVal t As
Threading.Threa dExceptionEvent Args)

'log exception

Helper.LogExcep tion(t.Exceptio n, "Unhandled exception.")

End Class

What code can I use to handle unhandled exception when developing a
Windows Service?
You add a handler to System.AppDomai n.CurrentDomain .UnhandledExcep tion
instead of Application.Thr eadException

Jun 25 '07 #2
On Jun 25, 4:15 pm, "Penelope Dramas" <penelopeDOTdra masATpro-
transportDOTcom wrote:
Hello,

I am using following code to handle unhandled exceptions in Windows Forms.

Shared Sub Main()

Dim GlobalException Handler As New GlobalException Handler

AddHandler Application.Thr eadException, AddressOf
GlobalException Handler.OnThrea dException

Application.Run (New MainForm)

End Sub

Exception Class:

Public Class GlobalException Handler

'Handle the exception event

Public Sub OnThreadExcepti on(ByVal sender As Object, ByVal t As
Threading.Threa dExceptionEvent Args)

'log exception

Helper.LogExcep tion(t.Exceptio n, "Unhandled exception.")

End Class

What code can I use to handle unhandled exception when developing a Windows
Service?

Thanks,

Pen D.
It is best not to use global exception handlers in Windows Services.
Due to the nature of a service any global handler may not handle the
exception correctly and the service might terminate before you are
notified of the exception. It is better to put open catch blocks where
you need them and at the base of all threads. You can create a static
exception handling class that will help you with this.

If you still want to use a global handler then you need to attach to
the UnhandledExcept ion event of the CurrentDomain.

AddHandler System.AppDomai n.CurrentDomain .UnhandledExcep tion,
AddressOf GlobalException Handler.OnUnhan dledException
Jun 25 '07 #3
Penelope Dramas wrote:
Shared Sub Main()
Dim GlobalException Handler As New GlobalException Handler
AddHandler Application.Thr eadException, AddressOf
GlobalException Handler.OnThrea dException
Application.Run (New MainForm)
End Sub
"Global" (Eeek!) Exception handlers like these should be regarded as a
back-stop; a way to log an terminal error just before your application
process finally keels over and dies - that classic cartoon scene where a
character runs off the edge of a cliff but hasn't started falling yet.

Don't expect them to be able to do anything more useful.
What code can I use to handle unhandled exception when developing a Windows
Service?
Windows Services, oddly, are far simpler beasts than Windows Forms apps,
so placing a Try .. Catch block wrapped around the main "Processing "
method for your Service will do the trick (unless you use a Timer, which
I don't recommend).

And, of course, you need them around the entry point(s) to any other
Thread(s) that you might spark off from within the Service - this is
particularly important if you're heading into Framework 2.0 and beyond,
where unhandled Exceptions in a Thread kill the entire process (unlike
1.1, where the thread just disappears, leaving the rest of the app
wondering where it went).

HTH,
Phill W.
Jun 26 '07 #4
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in
news:f5******** **@south.jnrs.j a.net:
Windows Services, oddly, are far simpler beasts than Windows Forms
apps, so placing a Try .. Catch block wrapped around the main
"Processing " method for your Service will do the trick (unless you use
a Timer, which I don't recommend).
Whats wrong with using a Timer in a Windows Service?

System.Timers.T imer was built specifically for use in a Windows Service.

It is debatable which is better - a loop vs. timer. I use both depending on
the situation.
Jun 26 '07 #5
First of all, thank you all for answers.

I do use timer, like most of the people, to do specific actions every x
milliseconds within windows service.
So far, I didn't hear that anything can be wrong with it.

As far as global exception handler, we can debate on how-to-implement-it but
you'll agree that it is much better to log/display error then to simply let
it "run off the edge of a cliff " without knowing what caused it.
"Spam Catcher" <sp**********@r ogers.comwrote in message
news:Xn******** *************** ***********@127 .0.0.1...
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in
news:f5******** **@south.jnrs.j a.net:
>Windows Services, oddly, are far simpler beasts than Windows Forms
apps, so placing a Try .. Catch block wrapped around the main
"Processing " method for your Service will do the trick (unless you use
a Timer, which I don't recommend).

Whats wrong with using a Timer in a Windows Service?

System.Timers.T imer was built specifically for use in a Windows Service.

It is debatable which is better - a loop vs. timer. I use both depending
on
the situation.

Jun 26 '07 #6
Spam Catcher wrote:
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in
news:f5******** **@south.jnrs.j a.net:
>Windows Services, oddly, are far simpler beasts than Windows Forms
apps, so placing a Try .. Catch block wrapped around the main
"Processing " method for your Service will do the trick (unless you
use a Timer, which I don't recommend).

Whats wrong with using a Timer in a Windows Service?
I read that as using a Try...Catch wrapper around a Timer in a Windows
Service was a bad idea.

Andrew
Jun 26 '07 #7
"Andrew Morton" <ak*@in-press.co.uk.inv alidwrote in news:#11sQ7AuHH A.536
@TK2MSFTNGP06.p hx.gbl:
I read that as using a Try...Catch wrapper around a Timer in a Windows
Service was a bad idea.
I haven't heard of that one before...
Jun 26 '07 #8
Spam Catcher wrote:
"Andrew Morton" <ak*@in-press.co.uk.inv alidwrote in news:#11sQ7AuHH A.536
@TK2MSFTNGP06.p hx.gbl:
>I read that as using a Try...Catch wrapper around a Timer in a Windows
Service was a bad idea.

I haven't heard of that one before...
The problem is that you /can't/ wrap a Timer-invoked routine in a Try ..
Catch, because the "calling routine" where the Try would have to go is
buried somewhere in the Framework.

See my previous posting (apologies for the sizable link):

http://groups.google.com/group/micro...8261c5ff2c5f2e

Regards,
Phill W.
Jun 27 '07 #9
"As far as global exception handler, we can debate on how-to-implement-it but
you'll agree that it is much better to log/display error then to simply let
it "run off the edge of a cliff " without knowing what caused it. "
If you deal with all the errors correctly then you will have logged
the fault before it runs off the cliff as you will catch all the
exceptions and handle them properly. This will probably also produce
logging that is far more useful in determining what the fault is. That
said it does not hurt to use the UnhandledExcept ion event for a simple
last resort logging of the error. You just should not rely on it as a
safety net and certainly do not use it to try and recover from the
exception.

Jun 27 '07 #10

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

Similar topics

2
9539
by: Steph | last post by:
I am working on a Windows Forms App. I have attached a standard UnhandledExceptionEventHandler to the current domain to catch unexpected errors. In my application, when I trigger an unhandled error, my handler does not get hit - I get the standard ugly run time error. The strange thing is, I tried the exact same code in a new, windows forms application, and when I trigger an unhandled exception, the code works fine (the JIT compiler...
2
5646
by: nunya5200-bidness | last post by:
I hope someone may have some insight to this issue... I would like to know how to properly implement exception handling using the ObjectDataSource control. Specfically, I am doing an insert using an object that calls a web service to accomplish this. The service returns a message to the Insert method that indicates that the insert can't be accomplished because it is trying to insert a duplicate key value. So, I simply want to raise...
7
2681
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote server returned an error: (500) Internal Server Error." I found a post that suggested to catch the WebException to retrieve the actual HttpWebResponse object for more information. The response returned is shown below. At first I thought this was a...
1
1867
by: GS | last post by:
Any points of what would be the good error handling design for application? User error handling in Application_OnError and throw() new errors on conditions through the code? I'd like utlimiately to consolidate all error_handling in one method which I'll be able to easily modify to write to event log or text file etc instead of error hanlding scattered through the code. Thanks, GS
2
2136
by: tshad | last post by:
This has been driving me crazy. I have been trying to get the error handling working on my system and can get parts of it working and others won't work at all. I found that you can't access session variables from Application_Error event, but I need to so I don't need to set up traces to see what they are during an error. I am emailing the error condition to myself when it happens.
16
2536
by: Chuck Cobb | last post by:
I'm implementing a centralized exception handling routine using the Enterprise Library Exception Management Application Block. I trap all unhandled exceptions to one place using the following method: // --- Create an Exception Handler for Thread Exceptions ---------------- Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);
0
1456
by: Mike S | last post by:
I'm still looking into this problem, but to summarize: I wrote a very simple Windows Service in VB.NET, as part of a larger application. I created a deployment project for the application the resulting MSI explicitly installs the service. I've ran the installer on 4 machines at our office (2 are running Windows 2000 Pro, 1 is running XP Pro, and the other is running Windows 98). On each of these machines, the installation goes without a...
3
3199
by: Larry Herbinaux | last post by:
I have built an asychronous TCP Server that uses the thread pool. I have two levels of exception handling in case the handling of the inner catch block throws an exception. The outer catch block does nothing put print a string literal to our logging mechanism. The general structure for this is: AsyncReceiveCallback { try { OnReceiveDelegate();
10
5588
by: Penelope Dramas | last post by:
Hello, I am using following code to handle unhandled exceptions in Windows Forms. Shared Sub Main() Dim GlobalExceptionHandler As New GlobalExceptionHandler AddHandler Application.ThreadException, AddressOf GlobalExceptionHandler.OnThreadException
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8747
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
8528
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
7356
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...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
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
2
1737
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.