For the purpose of control and improvement program. I need to catch the Exception.

I don't know what to do below:

1. Control exception what set in the program and send notice to the developer (via Email)

Best Regards!

Recommended Answers

All 14 Replies

try
{
   // do stuff here
}
catch(Exception exc)
{
   //Send mail here with contents of exc in the body
}

You don't understand me. My opinion is to build a function to manage Exception that choose to be emailed.

You would still catch the generic exception and pass it to the evaluator that would send email based on exception type.

Right?

I explain more about my request.
Program have many Exception check the validity of operations input data. Ex: Exception1, Exception2.

For the purpose of the above, I need to send mail notification about Exception2 for developer.

try 
{}
catch(Exception ex){MessageBox.Show(ex.Message);}

But a lot of Exception in the program, how to manage all Exception once?

Once?
You can put all of the code inside one try/catch block.

you can explain clear (better to have code samples) about:
catch(Exception exc)
{
//Send mail here with contents of exc in the body
}

Let's say you have a SendMail function like what is in this post.
..You would need to make the "class Program" a "public class Program" and add it as a reference to this program.

You can then do something like this:

using System;
//
namespace DW_400780
{
   using GetCred;
   class Program
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Hello, World");
         string strUserId = "UserJoe";
         //
         try
         {
            // Example
            throw new NotImplementedException("This feature has not been implemented");
         }
         catch (Exception exc)
         {
            string strError = "";
            try
            {
               //Send the StackTrace from the exception in email
               SendGMailCs.Program.SendMail(
                  //Credential from some source of your choosing
                  CGetCred.GetCred("GMAIL"),
                  // Title goes here
                  "Exception",
                  // Send to this address
                  "me@mydomain.com",
                  // User ID + The StackTrace is the body
                  (strUserId + '\n'+ exc.StackTrace),
                  // No attachment
                  "",
                  // string filled on error from SendMail
                  ref strError 
                  );
            }
            catch (Exception) {/*Ignore if mail cannot be sent*/}

            throw exc; // throw the original exception so user can see it.
         }
      }
   }
}

If it is a Windows app, then you can use the Application.ThreadException event:

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            Application.Run(new MainForm());
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
// add code to send email here
        }

The ThreadException event is raised whenever there is an untrapped exception (e.g. no try/catch is around the exception, or the exception occurs within a catch block).

Note that your application could be in an unstable state when the ThreadException is thrown, so you will probably want to respond to the event by sending the email and then exiting the application.

I explain more about my request.
Program have many Exception check the validity of operations input data. Ex: Exception1, Exception2.

For the purpose of the above, I need to send mail notification about Exception2 for developer.

try 
{}
catch(Exception ex){MessageBox.Show(ex.Message);}

But a lot of Exception in the program, how to manage all Exception once?

Hi thines01!

I understood! But I have a question about your email function: using GetCred;?
How to use GetCred?

Hi avertyn!

According to your instructions, The Application.ThreadException event can handle the exception in my application?

In my application the Exception to the management objectives validity of the data manipulation.

Hi avertyn!

According your instruction, the ThreadException event can handle the exception that related to manage the data manipulation in my application?

Example: I have used Exception to catch errors in input data.

Hi avertyn!

According to your instructions, The Application.ThreadException event can handle the exception in my application?

In my application the Exception to the management objectives validity of the data manipulation.

That is a personal library I use to get credentials from a repository. I also use it for demonstration code so I don't ever forget to remove account information before posting. You can simply replace it with a new NetworkCredential.

oh. Thanks thines01!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.