I can't catch exception on windows services.

I've already tried the following:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(handlerMethod);

alone and in combination with:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);

It doesn't even function with:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

I've also tried:

Application.ThreadException += new ThreadExceptionEventHandler(handlerMethod);

Have you tried

try { /* code */ } catch { /* fail code */ }

?

Have you tried

try { /* code */ } catch { /* fail code */ }

?

I mustn't use try and catch.

I can't catch exception on windows services.

Where is the exception coming from? That can be a significant reason why you aren't seeing it handled. For example, if your service creates a thread which then throws an unhandled exception, it won't be caught by the unhandled exception handler.

I mustn't use try and catch.

The unhandled exception handler is a hedge, not something you should use as a primary strategy.

Where is the exception coming from? That can be a significant reason why you aren't seeing it handled. For example, if your service creates a thread which then throws an unhandled exception, it won't be caught by the unhandled exception handler.

The exception is thrown from a method which is called by a timer.

Why can't you use try catch?

Can you show us the full code, where the exception occurs?
thx in advance.

Why can't you use try catch?

Because my boss said so :(.

Can you show us the full code, where the exception occurs?
thx in advance.

static class Program
    {
        static System.Timers.Timer t;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            t = new System.Timers.Timer();
            t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
            t.Interval = 300000;
            t.Enabled = true;

            ServiceBase[] ServicesToRun;

            // More than one user Service may run within the same process. To add
            // another service to this process, change the following line to
            // create a second service object. For example,
            //
            //   ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
            //
            ServicesToRun = new ServiceBase[] { new MyNewService() };

            ServiceBase.Run(ServicesToRun);
        }

        static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
                throw new NotImplementedException("exception message");// this is what I want to catch without try-catch
        }
    }

If your boss says you can't use try catch, then just let the application fail.

By setting exception handlers at the entry level, all you're effectively doing is wrapping your entire application in a try catch block anyway.

To ask for no try/catch is to me, like asking for no exception handling.

My only reason for having the unhandled exception handler is to allow for a graceful shut down. You have no idea what "state" your application is in. It's unreasonable, to me, to ask that you continue to provide a service with your application if you've caught an unhandled exception.

Timer.Elapsed method will be rised when The time set in the Interval peopery will go by. So is there any special code in the Elapsed method to catch?
Because from this upper code you pased, I cant se eany reason why to catch exception (or any error), becuase there isn`t any.

Because my boss said so

WTF. First, why is your boss dictating how you handle exceptions? Second, why is your boss dictating things that he clearly has no clue about?

Anyway, you're throwing the exception from a different thread (created for the timer handler). The unhandled exception is being swallowed by the CLR rather than propagated to the unhandled exception handler. You could try setting up an unhandled exception handler for the thread in t_Elapsed...but seriously, just use a try..catch.

Please delete this thread.

Please delete this thread.

Why? We don't delete threads without good reason, especially when there are replies.

Could you not just use an if then else statement for this? Sorry if I am wrong, I am not amazing in C# but would this not work? For example:

if (//Your Code Here)
{
   //Code
}
else
{
   MessageBox.Show("An error has occured.");
}

This wouldn't give you the exact reason it was not functioning correctly but it would tell you something went wrong.

Why? We don't delete threads without good reason, especially when there are replies.

Yep, you intentioanlly created this thread, with some "interesting" topic.
If you found your answer, please mark the thread as answered (you can even add some rep. point to individuals), so this thread will be closed.

PS: If your boss doesn`t let you using try, catch blocks, tell him he doesn`t have a clue about programming. And so you can close this thread as well.

Could you not just use an if then else statement for this?

How would you propose catching an already thrown exception with an if statement in the absence of a try...catch? ;)

Impossible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.