Lars Hansen
Lars Hansen

Reputation: 153

UnhandledException in windows service c#

Im having some real trouble. I really hope that someone is able to help me out here.

I've got a Windows Service, which is supposed to run with a certain frequence. My Service communicates with some webservices from our suppliers.

My problem is making my service robust, in case those services are unavailable, due to breakdown etc.

I've tried to control this with a try catch.

/***************************************************************************************************/
/* Code snippet: Unable to compile. (it says: The name 'xyz' does not exist in the current context)*/
try  
{
  var xyz = new xyz.ExternalService().GetRelevantData();
}

catch(Exception e)
{
  ...handle the exception
}
/* Code snippet: Unable to compile. (it says: The name 'xyz' does not exist in the current context)*/
/***************************************************************************************************/

But the compiler won't accept this (it says: The name 'xyz' does not exist in the current context), hense I need another solution. I bumped into currentDomain.UnhandledException and I would like to use this functionality. Unfortunately I can't get this to handle the exception.

Can anyone help me regarding a solution?

Regarding the code below with the division by zero, it's my intension to replace the division with:

var xyz = new xyz.ExternalService().GetRelevantData();

I belive that would solve my problem.

/*********************************************************************/
/* Code snippet: currentDomain.UnhandledException : Division by zero.*/

    public void divisionbyzero()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

        int i = 0;
        int m = 12;
        double x = 0;
        try
        {
            x = m * i;
        }
        catch (Exception e)
        {
            writeToLog(e.ToString());                
        }
        x = m / i;
    }
/*********************************************************************/
/* Code snippet: currentDomain.UnhandledException : Division by zero.*/

I would like to catch the exception with the 'UnhandledException', when the division is done. But unfortunately this doesn't happen.

New section with code context below: I hope this makes sense:

/*****************************************************************/        
/****Code snippet: called from Windows service OnTimedEvent *****/        
    static void MyHandler(object sender, UnhandledExceptionEventArgs args)
    {
        Exception e = (Exception)args.ExceptionObject;
        File.AppendAllText("C:\\lab\\logs\\log.txt", "" + e.Message + ""); 
    }

    private void writeToLog(Exception e)
    {
        File.AppendAllText("C:\\lab\\logs\\log.txt", "" + e.Message + "");
    }

    public void divisionbyzero() // this is called in Windows service in OnTimedEvent.
    {

        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);


        int ii = 0;
        int mm = 12;
        double xx = 0;
        try
        {
            xx = mm * ii;
        }
        catch (Exception excep)
        {
        File.AppendAllText("C:\\lab\\logs\\log.txt", "" + excep.ToString() + ""); 
        }

        xx = mm / ii; // This is the exception I would like (currentDomain.UnhandledException) to handle
    }
/****Code snippet: called from Windows service OnTimedEvent ************/        
/***********************************************************************/ 

I've been searching the internet: This guy has the same problem, you may want to read this: http://go4answers.webhost4life.com/Example/getting-appdomainunhandledexception-74589.aspx

Upvotes: 0

Views: 571

Answers (3)

Lars Hansen
Lars Hansen

Reputation: 153

Problem solved:

var xyz = new xyz().ExternalService();
try  
{
    xyz.GetRelevantData();
}

catch(Exception e)
{
    ...handle the exception
}

Thanks for all your posts :-).

Upvotes: 0

spender
spender

Reputation: 120380

So it looks, from your comments, that this is your problem:

try  
{
  var xyz = new xyz.ExternalService().GetRelevantData();
}

catch(Exception e)
{
  //but I want to use xyz here
  ...handle the exception
}

So, define xyz in a higher scope, so it's visible in the catch;

var xyz = new Xyz();
try
{
    var someValue = xyz.DoSomething();
}
catch(Exception e)
{
    //xyz is still visible here.
}

Upvotes: 1

Neel
Neel

Reputation: 11721

You need below code to create instance:

var xyz = new xyz().ExternalService().GetRelevantData();

Now xyz would be treated as a function! And i guess you are having that function in your code!

Then put it as below:

try  
{
  var xyz = new xyz().ExternalService().GetRelevantData();
}

catch(Exception e)
{
  ...handle the exception
}

Upvotes: 1

Related Questions