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?