Click here to Skip to main content
15,911,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
First of all,My project contains too many “try catch”,I worried that there is no complete exception message prompt,So I troubleshoot the error code position is very difficult。
I want to get an event that captures global exception information, and can get the complete exception information from "try catch"

In My project, there are too many lazy code

As follows:

C#
try
{
   //....throw a errr
}
catch{}



C#
try
{
  //....throw two  errr
}
catch(excetion ex)
{
   messagebox.show(ex.message);//but i want to know Stack
}


Such code too much

I need to see detailed information on the stack
Posted
Updated 4-Jun-15 5:35am
v2
Comments
Sergey Alexandrovich Kryukov 4-Jun-15 17:34pm    
From this question, I'm not sure that you understand exception propagation. If you have too many try-catch, its wrong. Each stack needs just one of just very few. Catching exception locally is a very bad thing. The best handling in no handling, letting the exceptions go.
—SA

To debug your application, you should run it under a debugger configured to stop on all exception as a starting point.

As indicated in a comment, you should avoid local try-catch when not necessary. You rarely need try/catch blocks in C#.

Usually, you might want to handle unhandled exception handling at application scope. A lot of information can be found on the web and in official MSDN documentation. By the way, since you are a beginner, it might make sense to read some books on C# first.

http://www.csharp-examples.net/catching-unhandled-exceptions/[^]

Both of you sample code are usually wrong. You rarely want to swallow exception as in first example and if you write a message to the user, you will typically give him a proper message and not use the original message.

For some type of application were safety or intellectual property are important, you really don't want to display the stack trace as it can give some crucial information.
 
Share this answer
 
Comments
fengyuchun88 5-Jun-15 4:13am    
I agree with your suggestions, thank you very much!
You can display the stack trace to the user in a MessageBox object.

C#
try
{
  // Code or throw statement
}
catch(Exception ex)
{
  MessageBox.Show(ex.StackTrace);
}
 
Share this answer
 
v2
My issue has been resolved, Thank you all! close
 
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