Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

Do you all have any ideas how to display the number of exceptions thrown for one application?

Do we use performance counter class?

I did a search about it, but do not really understand how to use it.
Do you have any idea how to call the application(.exe)and count the exception thrown for the application?

Appreciate your feedback.

Thanks!
Posted
Updated 31-Jul-11 21:25pm
v2
Comments
Dalek Dave 1-Aug-11 3:25am    
Edited for Grammar and Readability.

The question is not 100% correctly formulated, because the total amount of exception depends on how you process them. It is very typically to process exception for some purpose and then re-throw it, possibly throwing exception of different type. Strictly speaking, an attempt to count all exceptions may lead to changing the number of thrown exceptions.

Now, you could not find anything specific about your question simply because nearly nobody does such weird thing as counting exceptions. The structural exception handling is designed for very different purposes.

So, the prerequisite for answering your question is the condition that all exceptions are already handled "properly". Most important thing is that the whole application is fault-tolerant, so no exception cause application termination. In particular, it means that all exceptions are handled on the very top of each thread, and if the application can throw exception in cycle, there is a handler of all of the exception in cycle, which is typical for UI. WPF and System.Windows.Forms provides the mechanism for handling all exception in the main UI cycle.

Please see my past answers for further detail:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^].

Now, you need to take into account all cases where you catch exceptions in between. It is not recommended to prevent propagation of the exceptions up the stack but is necessary in some cases. You should locate all such cases and increment your exception counter.

As to the exception counter itself, you apparently need to use singleton pattern with thread synchronization capability (if you use threads), so the counter would be accessed from all parts of code in all threads.

See:
http://en.wikipedia.org/wiki/Singleton_pattern[^],
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

If you have more than one application domain, you should count exceptions separately and use some IPC methods to communicate between Application Domain.
For more information, see: System.AppDomain, http://msdn.microsoft.com/en-us/library/system.appdomain.aspx[^]. I don't want to go into further detail as I cannot be sure you use Application Domains. In most cases they are not used, so any extra information won't be useful. In case you do, you can learn about communication of Application Domains or ask your follow-up question, if you don't — just forget about it for now.

—SA
 
Share this answer
 
Comments
Syuhairah 1-Aug-11 3:02am    
Thanks SAKryukov for the feedback.
the solution that was provided is not really what i want. Maybe my explanation was not clear. Actually i want to create one application that can count the number of exception thrown in another application by using ".net clr exception". I manage to found the sample in msdn and did try it as below. not sure it was ok or not..
Sergey Alexandrovich Kryukov 1-Aug-11 3:33am    
Have you ever heard of isolation between processes? It is not possible unless you make your "another application" to cooperate.
If this is some arbitrary application -- there is no way, period.
--SA
Syuhairah 1-Aug-11 4:08am    
not sure u got what i mean above. I just want to count the exception thrown for another application only. do refer below site:
http://msdn.microsoft.com/en-us/library/kfhcywhs%28v=vs.71%29.aspx

anyway..thanks for the feedback ;)
Asish Limbu 1-Aug-11 3:29am    
Good Stuff. Bookmarked for future ref.
Sergey Alexandrovich Kryukov 1-Aug-11 3:31am    
Thank you, Asish.
--SA
C#
static void Main(string[] args)
        {
            string machineName = "srahifah";
            string categoryName = ".NET CLR Exceptions";
            //string counterName = "# of Exceps Thrown";
            string instanceName = "TestApp";
            PerformanceCounterCategory pcc;
            PerformanceCounter[] counters;
            // Copy the supplied arguments into the local variables.
            try
            {
                categoryName = args[0];
                machineName = args[1] == "." ? "" : args[1];
                instanceName = args[2];
            }
            catch
            {
                // Ignore the exception from non-supplied arguments.
            }
            try
            {
                // Create the appropriate PerformanceCounterCategory object.
                if (machineName.Length > 0)
                {
                    pcc = new PerformanceCounterCategory(categoryName, machineName);
                }
                else
                {
                    pcc = new PerformanceCounterCategory(categoryName);
                }

                // Get the counters for this instance or a single instance
                // of the selected category.
                if (instanceName.Length > 0)
                {
                    counters = pcc.GetCounters(instanceName);
                }
                else
                {
                    counters = pcc.GetCounters();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to get counter information for " +
                    (instanceName.Length > 0 ? "instance \"{2}\" in " : "single-instance ") +
                    "category \"{0}\" on " + (machineName.Length > 0 ? "computer \"{1}\":" : "this computer:"),
                    categoryName, machineName, instanceName);
                Console.WriteLine(ex.Message);
                return;
            }
            // Display the counter names if GetCounters was successful.
            if (counters != null)
            {
                Console.WriteLine("These counters exist in " +
                    (instanceName.Length > 0 ? "instance \"{1}\" of" : "single instance") +
                    " category {0} on " + (machineName.Length > 0 ? "computer \"{2}\":" : "this computer:"),
                    categoryName, instanceName, machineName);
                // Display a numbered list of the counter names.
                int objX;
                for (objX = 0; objX < counters.Length; objX++)
                {
                    Console.WriteLine("{0,4} - {1}", objX + 1, counters[objX].CounterName);
                    Console.WriteLine("{0,4} - {1}", objX + 1, counters[objX].RawValue);
                }
                Console.ReadKey();
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900