C #-catch (Exception ex) will discard StackTrace. Why?

Source: Internet
Author: User

StackTrace: stores the stack call information of a method. What does it mean? Method A calls Method B, and method B calls method C. When you call method A, the StackTrace is probably like this: at Project. class. C in c: \ aaa \ Project \ class. cs: line 10.at Project. class. B in c: \ aaa \ Project \ class. cs: line upload at Project. class. A in c: \ aaa \ Project \ class. cs: line 30. it is a string. But what is his use? What do you mean, after all, people even tell you the line number. If StackTrace is lost, we will lose this information. Under what circumstances will StackTrace be lost? Take a look at this Code:

static void Main(string[] args)         {             try             {                 // Call Method1                 Console.WriteLine(Method1());             }             catch (Exception ex)             {                 Console.WriteLine(ex.StackTrace);             }             Console.ReadLine();         }           public static int Method1()         {             try             {                 return Method1_1();             }             catch (Exception ex)             {                 throw ex;             }         }           public static int Method1_1()         {             int j = 0;             return 10 / j;         }  

 

At first glance, there seems to be no problem, but Method1 does one more thing: Catch (Exception ex) {thorw ex;} He has a consequence that StackStace will be lost. the StackTrace obtained is as follows: at ExceptionMethodCall. program. method1 () in c: \ Projects \ ExceptionMethodCall \ Program. cs: line 33 at ExceptionMethodCall. program. main (String [] args) in c: \ Projects \ ExceptionMethodCall \ Program. cs: line 16 Delete try .. catch, The StackTrace we get is as follows: at ExceptionMethodCall. progra M. method1_1 () in c: \ Projects \ ExceptionMethodCall \ Program. cs: line 40 at ExceptionMethodCall. program. method1 () in c: \ Projects \ ExceptionMethodCall \ Program. cs: line 29 at ExceptionMethodCall. program. main (String [] args) in c: \ Projects \ ExceptionMethodCall \ Program. cs: line 16 results are obvious. Conclusion: try... catch cannot be added in the future?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.