Question

I've developed an application used by a third-party company. Since I'm a horrible coder the application does still have some bugs which causes it to crash (unhandled nullpointerexception for example).

It's a Windows-forms application running on .NET 4.5 and now they are just getting the classic "An unhandled exception caused the app to terminate, press details for more info".

Trying to convince them that pressing "Details" and sending the stack-trace to me is really useful but they all seem reluctant.

Would it be possible to automate this behaviour, like show them a custom global "Exception catcher" where they can just press a button to send it to me by E-mail.

Inbefore "Global exception handling is bad" and "Why does your application throw nullpointerexceptions, you are a bad coder etc."

BR Tomas Anyuru

Was it helpful?

Solution

I guess the exceptions you get are unhandled.

Because of this, you will have to use the Application.ThreadException event to handle them. Because there is no .NET automatic mail sending and message display, you will have to implement your own inside this event.

Please have a look of an answer I wrote to have some examples of Exception catching strategies.

OTHER TIPS

wrap your whole main() function in try-catch statement.
this way any un-handled exception will roll back and will be catched in your catch block:

static void main()
{
    try
    {
        // the application code...
    }
    catch (Exception ex)
    {
        DialogResult result = MessageBox.Show(
        "Some error occured, please click ok to send it to the develpoer");
        if (result = OK)
            email(ex); // this is your function to send the email.
                        // useful information is also in ex.message
        // here program will exit without error!
    }
}

you can use log 4 net it is open source logging tools, use a lot by Java developer, and this version is specially for .Net http://logging.apache.org/log4net/

In addition to @Shamim code, you can wrap your main function in try, catch block, since the catch block here will track down the exception occurred inside any function called in the try block.

Shooting a mail inside catch block sometime throws and exception about Thread abort, so finally would be the right place to do so :

catch (Exception err)
                {
                    mailBody = "Error: " + Convert.ToString(err.Message) + "<br /> Source: " + Convert.ToString(err.Source);
                    //Can display some message to user in an Literal Control from here.
                }
                finally 
                {
                    if (!string.IsNullOrEmpty(mailBody))
                    {
                        mailObject.To.Add(mailTo);
                        mailObject.CC.Add(mailCc);
                        mailObject.Body = mailBody;

                        MailService(mailObject);
                    }
                }

MailService is a method to send mail which accept a MailObject as parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top