0

we have one console application in which we have another class library that contains all the business functions in which we are using in main method of program file into that we are getting errors but that is not able to handle.

I find below code to add in program file to handle exception and create handler MyExceptionHandler.

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

In my project this only works when I have exception in Main method of program file, but when I have exception from Method of business function class it's not handling it.

Please suggest a better way to handle global exception.

.NET Global exception handler in console application

In this question they have different question then this as i already mention i already tried that solution. I am able to call it when it gives error in same program file method but when there is exceptions from another class library then its simply get back to main parent method.

Community
  • 1
  • 1
user Tbs
  • 11
  • 5
  • Possible duplicate of [.NET Global exception handler in console application](http://stackoverflow.com/questions/3133199/net-global-exception-handler-in-console-application) – Murat Gündeş Mar 14 '17 at 13:05
  • No, they have different question then this as i already mention i already tried that solution. I am able to call it when it gives error in same program file method but when there is exceptions from another class library then its simply get back to main parent method.. – user Tbs Mar 14 '17 at 13:11
  • Not a duplicate. The OP has an other class library which is actually throwing exceptions. – mmushtaq Mar 14 '17 at 13:12
  • Exactly, this is actual issue @mmushtaq – user Tbs Mar 14 '17 at 13:13
  • Are you running your program in VS. In debug mode Studio tries to handle all the exceptions. – DotNet Developer Mar 14 '17 at 13:17
  • Yes we are running application in VS 2013 in debug mode @Bahrom – user Tbs Mar 14 '17 at 13:19

2 Answers2

3

I tried experiment below which I staged as you explained and everything is working.

....

// in ClassLibrary1 project
using System;    

namespace ClassLibrary1
{
    public class Class1
    {
        public void Method()
        {
            throw new Exception("Unhandled exception");
        }
    }
}    

....

// in ConsoleApplication1 project
// ClassLibrary1 must be referenced
using System;    

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyExceptionHandler);

            ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1();
            class1.Method();
        }

        static void MyExceptionHandler(object sender, UnhandledExceptionEventArgs e)
        {// Break point here is hitting
            // ... Processing exception ...
        }
    }
}

See if I did exactly as what you meant.

DotNet Developer
  • 2,973
  • 1
  • 15
  • 24
  • My Application is as same as you staged it, But in that i have one another method in class Class1 like Method2 from method method2(), I am calling method Method(). Now problem is I am not getting exception when I have exception in Method Method2(). So I currently solve it by adding try catch in Method2() and manually call exception handler in Catch block. But I don't want to use try catch over there. If You can suggest any better way then please suggest me, I will be thankful to you. – user Tbs Mar 15 '17 at 13:24
  • I don't know what you planned to do with this event but the documentation says that this event has special purpose, for example to log info about the exception and save program data before termination. What I know is try and catch block which you have already been suggested. – DotNet Developer Mar 15 '17 at 13:34
  • I have lots of lines code in Methods so I have to divide it in small parts and then processed it. Yes Exactly I am tracing Exception and used it to send mail regarding it before termination. I already use try catch at lowest level but if there is any other way then it, then i will like to go with that. – user Tbs Mar 15 '17 at 13:50
0

Why don't you just write a try-catch statement surrounding your logic in the main() method?

i.e.:

public void main(string[] args){
    try{
        // your logic that throws exceptions here (even calls to other dll's)
    catch(Exception e){
        // your exception handling logic here
    }
}

Yet I may not understand your question completely as this is pretty basic stuff.

Peter
  • 14,221
  • 15
  • 70
  • 110