Handling unhandled exception in .net service

P

Penelope Dramas

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

Application.Run(New MainForm)

End Sub

Exception Class:

Public Class GlobalExceptionHandler

'Handle the exception event

Public Sub OnThreadException(ByVal sender As Object, ByVal t As
Threading.ThreadExceptionEventArgs)

'log exception

Helper.LogException(t.Exception, "Unhandled exception.")

End Class



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



Thanks,



Pen D.
 
J

John Vottero

Penelope Dramas said:
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

Application.Run(New MainForm)

End Sub

Exception Class:

Public Class GlobalExceptionHandler

'Handle the exception event

Public Sub OnThreadException(ByVal sender As Object, ByVal t As
Threading.ThreadExceptionEventArgs)

'log exception

Helper.LogException(t.Exception, "Unhandled exception.")

End Class



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

You add a handler to System.AppDomain.CurrentDomain.UnhandledException
instead of Application.ThreadException
 
O

Olie

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

Application.Run(New MainForm)

End Sub

Exception Class:

Public Class GlobalExceptionHandler

'Handle the exception event

Public Sub OnThreadException(ByVal sender As Object, ByVal t As
Threading.ThreadExceptionEventArgs)

'log exception

Helper.LogException(t.Exception, "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 UnhandledException event of the CurrentDomain.

AddHandler System.AppDomain.CurrentDomain.UnhandledException,
AddressOf GlobalExceptionHandler.OnUnhandledException
 
P

Phill W.

Penelope said:
Shared Sub Main()
Dim GlobalExceptionHandler As New GlobalExceptionHandler
AddHandler Application.ThreadException, AddressOf
GlobalExceptionHandler.OnThreadException
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.
 
G

Guest

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.Timer 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.
 
P

Penelope Dramas

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.
 
A

Andrew Morton

Spam said:
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
 
O

Olie

"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 UnhandledException 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.
 
J

John Vottero

Phill W. said:
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/micr...exception+phill&rnum=1&hl=en#258261c5ff2c5f2e

The problem you describe in that posting has less to do with timers than it
does with threads and V1.1 of the Framework. In V1.1 of the Framework, if a
thread started by a service fails, it just quietly drops dead leaving the
service running. That's fixed in V2.0 of the Framework (the whole service
will fail now).

I have no troubles with timers in services.
 

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