0

I am trying to find a way to make all the exceptions in a console application to pass trough a IErrorHandler (I use this Denomination because in wcf this kind of operation is possible to do with a IErrorHandler).

I searched on google but all the results seem to return only WCF solutions.

What I'm looking for is a solution a little bit like this but for a console application, in WCF it looks easy because you can configure a behavior for each endpoint but in a windows application I think that this kind of solution can also be reachable.

All the exceptions are cached, but this is legacy code and calling a method to handle a exception in every catch is not a solution for my problem.

Thanks in advance.

ragerory
  • 1,360
  • 1
  • 8
  • 27
Diogo Cunha
  • 1,194
  • 11
  • 23
  • 1
    When its for a console application, why are you mentioning ASP.Net? – user2900970 Jun 02 '15 at 18:22
  • Did you even try googling for C# Console App Global Exception Handler? I did, and this was the first result: http://stackoverflow.com/questions/3133199/net-global-exception-handler-in-console-application – thorkia Jun 02 '15 at 18:59
  • No such black voodoo magic exists in .NET. You can only get a notification through AppDomain.UnhandledException but you cannot handle the exception, your program always terminates. – Hans Passant Jun 02 '15 at 19:21

1 Answers1

1

You can either bubble all your exceptions up to the root of your console application and wrap in a big try/catch that handles all exceptions, or at the start of your console application you can add an event handler to your app domain for "Unhandled Exception" event and all exceptions that go unhandled will get routed there.

using System;
using System.Security.Permissions;

public class Example 
{
   [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
   public static void Main()
   {
      AppDomain currentDomain = AppDomain.CurrentDomain;
      currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

      try {
         throw new Exception("1");
      } catch (Exception e) {
         Console.WriteLine("Catch clause caught : {0} \n", e.Message);
      }

      throw new Exception("2");
   }

   static void MyHandler(object sender, UnhandledExceptionEventArgs args) 
   {
      Exception e = (Exception) args.ExceptionObject;
      Console.WriteLine("MyHandler caught : " + e.Message);
      Console.WriteLine("Runtime terminating: {0}", args.IsTerminating);
   }
}

source

Update

Not a lot of great simple options for doing what you're trying to do. One thing you could try out is installing msysgit / cygwin and use unix tools to find replace all occurrences. This would be pretty experimental and you'd have to do some heavy testing.

find <project dir> -type f -exec sed -i '.bak' 's/catch\(Exception ex\)\r\n{/catch\(Exception ex\)\r\n{\r\nModernExceptionHandler.handle(ex);\r\n/g' {} +

Which should recursively walk your project directories and replace occurrences of -

catch(Exception ex)
{

with

catch(Exception ex)
{
    ModernExceptionHandler.handle(ex);

Files that get modified will have a backup version saved alongside them with .bak file extension.

More info on find / sed

Community
  • 1
  • 1
cchamberlain
  • 17,444
  • 7
  • 59
  • 72
  • Thanks for the answer but maybe I didn't explain correctly, in my last sentence I said that all the exceptions are handled and unhandle all exceptions is not a solution because this is legacy code, not a simple app – Diogo Cunha Jun 02 '15 at 19:15
  • @DiogoCunha - Updated with some magic only possible with unix based tools installed. – cchamberlain Jun 02 '15 at 19:48