0

I have a c# console application. I also have windows task scheduler which I use to execute my exe file every morning.

I put a try catch block (shown below) to capture any errors and write the exception to a text file.

The actual process in the try block creates an excel instance and reads some data from a workbook. The code normally works. However when it fails the error never seems to be caught.

I have other applications which use the same code in the catch block to output the exception and know that this part works. Just seems the exception isn't be caught. I can see that the task scheduled started the file.

Update

I have checked the event viewer (thanks to a comment below) and can see that the task was successfully started

 class Program
{
    static void Main(string[] args)
    {
        try
        {
            // do some work
        }
        catch(Exception ex)
        {
            // write output to log file
        }
    }
  }
mHelpMe
  • 6,336
  • 24
  • 75
  • 150
  • Do you use any sort of async programming in try block? – Goran Ćojanović Sep 27 '18 at 08:29
  • 1
    Is it that the error is never caught, or that your log file isn't working. figure this out and you will probably have your answer. unless of course its a stakoverflow or something – TheGeneral Sep 27 '18 at 08:30
  • The problem can be in the `// do some work` code. When the code runs in another thread, then it is possible that the `catch` in existing code will not catch the exception. For WinForms there is a event for exception in another thread _(when I remember correctly)_. Unfortunately I'm not sure how to handle this in console. And of course, there are errors, that are not caught for newer frameworks _(e.g. memory errors)_. – Julo Sep 27 '18 at 08:30
  • @GoranĆojanović, no I'm not using any async programming, its a pretty basic task and pretty quick – mHelpMe Sep 27 '18 at 08:32
  • 1
    What sort of exception are you 'expecting'? Can't recall, but I think Excel interop is exposing `Excel` errors in some kind of object? – Mike Sep 27 '18 at 08:32
  • @Saruman I'm pretty confident that my log file is fine, it works for all my other applications. Sometimes we have issues opening an excel instance becuase of the various add-inns excel has (which are not needed for this application) – mHelpMe Sep 27 '18 at 08:32
  • Is there anything in the event log – TheGeneral Sep 27 '18 at 08:33
  • @Saruman nope nothing in the log file, don't believe it even gets there – mHelpMe Sep 27 '18 at 08:34
  • @Mike, not sure to be honest what exception I'm expecting to see but believe it will be to do with Excel – mHelpMe Sep 27 '18 at 08:34
  • 1
    I mean event viewer – TheGeneral Sep 27 '18 at 08:35
  • @Saruman, ah didn't think of that. Where would I find it in the event viewer? – mHelpMe Sep 27 '18 at 08:39
  • 1
    [6 Ways to Open Event Viewer in Windows 10](https://www.isunshare.com/windows-10/6-ways-to-open-event-viewer-in-windows-10.html) – TheGeneral Sep 27 '18 at 08:39
  • @Saruman, I have seen that the tasked was succesfully created in the event viewer – mHelpMe Sep 27 '18 at 08:48
  • Is there any chance that your different apps using same file for logging the issues. If thats the case then may be just may be file is locked by some other thread at the same time. – Mahesh Sep 27 '18 at 09:57
  • @CoderofCode no they all have separate files – mHelpMe Sep 27 '18 at 13:37

1 Answers1

0

if the process of the excel creation is on a different thread or any part of it than the main thread, then the try-catch block won't catch the exception and you need to use UnhandledException

based on this https://stackoverflow.com/a/3133249/2696230

adjust your code to the following:

class Program
{
    static void Main(string[] args)
    {
           System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
        try
        {

            // do some work
        }
        catch(Exception ex)
        {
            // write output to log file
        }
    }

    static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) {
      // handle the expception
    }
  }
Abdulkarim Kanaan
  • 1,703
  • 4
  • 20
  • 32