Miraj Baldha
Miraj Baldha

Reputation: 307

Adding further exception handling to an application with a single exception handler

I have just made one desktop application using

Technology:

Patterns:

Now i required to add exception handling mechanism, currently my application have no try catch anywhere except in Main(). Where i put try catch, and handled all types exception in Global exception handler(ApplicationThreadException). but this is not proper way to handle exception.

So any ideas or any library or anything which can help me for handling exception?

Upvotes: 1

Views: 210

Answers (5)

houssam11350
houssam11350

Reputation: 216

I always use this code :

public static void HandleException(Exception  e)
        {
            string s="Message= "+e.Message +"\n";
            s+="Source= "+e.Source +"\n";
            s+="Stack Trace= "+e.StackTrace+"\n";
            Exception inner=e.InnerException;
            MessageBox.Show(s);
            while(inner !=null)
            {
                string ss="Message= "+inner.Message +"\n";
                ss+="Source= "+inner.Source +"\n";
                ss+="Stack Trace= "+inner.StackTrace+"\n";
                MessageBox.Show(ss);
                inner=inner.InnerException;


            }
        }

Upvotes: 0

razlebe
razlebe

Reputation: 7144

So any ideas or any library or anything which can help me for handling exception?

I think you're attempting to shut the barn door after the proverbial horse has bolted.

Exception handling isn't something you can easily bolt-on as an afterthought. Consideration should be given to exception handling as you build your application. For every functional unit of code that you write you should carefully consider whether exceptions could be generated and if so identify the appropriate place to handle them. Certainly there's no magic library you can just plug in.

Now i required to add exception handling mechanism, currently my application have no try catch anywhere except in Main(). Where i put try catch, and handled all types exception in Global exception handler(ApplicationThreadException). but this is not proper way to handle exception.

In catching all exceptions in main, you've (so far) made the decision that your app doesn't need to deal with them at any other level. Is that actually the case? Ask yourself, whether your application currently functions well and does what it's supposed to do without constantly erroring out. Do you really need to add all of that exception-handling code?

  • If your app functions well now and you don't see any problems, then the answer is probably no: you've already got all of the exception handling that your currently need.

  • However, if your app is buggy and regularly fails with some exception or other being caught in main() and you're thinking to yourself that the whole app shouldn't fail just because of recoverable error, then you've identified a case for exception handling elsewhere, and you should go back and identify the individual failure cases and address them at the appropriate level in your app.

Upvotes: 4

Matt Cofer
Matt Cofer

Reputation: 2982

Wire up event handlers to handle unhandled exceptions anywhere in your application and then don't use a try/catch in main.

Try this in your Main():

    //Wire up handling of all unhandled exceptions
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    //Handle the events
    static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        HandleException(e.Exception);
    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        HandleException((Exception)e.ExceptionObject);
    }

    public static void HandleException(Exception e)
    {
         //Do something with the exception stored in e.
         //Email an admin, show the user a pretty message, show the user a detailed stack trace only if they click Show Details, etc.  

    }

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44605

Your way is surely not proper because you catch the exception far away from the point where it was thrown. And your global catch can eventually log but is unable to react.

Imagine situation where you try to create a file in a certain folder before manipulating it, it could be that you decide to catch the exception if the folder does not exist and you first create the folder then create the file in it. In the Main method is too late because you can't do anything, you can log yes but you can't create the folder, even if you do, the program will not continue from the proper point anyway.

This is veeeery vague introduction to why to do proper exception handling, just an example out of very many... just put your try catch everywhere needed and either react to exception (handle them) or throw them (sometimes from inside a catch. sometimes omitting the catch clause at all)... you should read some articles on this I think.

Upvotes: -1

Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

Only the logic of operation of your application tells you where to use try/catch.

If you can do something in case of exception - then you put the statement at that point. If in an entire app you didn't feel like you needed exception handling, because there were simply nothing you could do with them - outermost catcher is a good place for your exception handling, but would only serve logging purposes.

Upvotes: 3

Related Questions