1

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

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
taracus
  • 393
  • 1
  • 5
  • 19

4 Answers4

2

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.

Community
  • 1
  • 1
Larry
  • 17,605
  • 9
  • 77
  • 106
  • Thank you I belive this is what I was looking for. Going to have a look but marking your answer as correct. – taracus Mar 03 '14 at 19:10
2

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!
    }
}
Shamim
  • 434
  • 4
  • 11
  • Is this really possible, running everything under a try-block is ok? – taracus Mar 03 '14 at 19:09
  • you can try! ;) it's ok! though any logical bug or exception in any deep level of your program will cause to leave that place and end up right in the catch statement, therefor, after the catch statement is finished, program will end. you can use this more in your functions and deeper in your program anywhere you find it's necessary! – Shamim Mar 10 '14 at 10:12
0

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/

Alaa Abuzaghleh
  • 1,023
  • 6
  • 11
0

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.

foo-baar
  • 1,076
  • 3
  • 17
  • 42