C #-catch (Exception ex) will throw away StackTrace what's going on?

Source: Internet
Author: User
Tags try catch

Originally this article would like to write about how StackTrace can be lost, but now the content has become a discussion of how to deal with the problem of exception.

Should not use try catch, when use? Also bothered me for a long time, as if casually write can, but in fact there is best practise, the following content please refer to, welcome correction!


StackTrace: The stack invocation information for the Save method.

What do you mean? A method called the B method, the B method called the C method, you call the A method when the StackTrace is probably the case:

At PROJECT.CLASS.C in C:\aaa\project\class.cs:line 10.

At project.class.b in C:\aaa\project\class.cs:line 20.

At PROJECT.CLASS.A in C:\aaa\project\class.cs:line 30.

It's just a string.

But what's the use of him? You say, after all, they even tell you the line number. If we lose the stacktrace, we will lose the information.

Under what circumstances will stacktrace be lost? Take a look at this piece of code:

[CSharp]View PlainCopyprint?
  1. static void Main (string[] args)
  2. {
  3. Try
  4. {
  5. //Call Method1
  6. Console.WriteLine (Method1 ());
  7. }
  8. catch (Exception ex)
  9. {
  10. Console.WriteLine (ex. StackTrace);
  11. }
  12. Console.ReadLine ();
  13. }
  14. public static int Method1 ()
  15. {
  16. Try
  17. {
  18. return method1_1 ();
  19. }
  20. catch (Exception ex)
  21. {
  22. throw ex;
  23. }
  24. }
  25. public static int method1_1 ()
  26. {
  27. int j = 0;
  28. return 10/j;
  29. }


At first glance there seems to be no problem, but Method1 did one more thing: Catch (Exception ex) {THORW ex;}

He brought a consequence that Stackstace would be lost.

The resulting stacktrace are as follows:

At ExceptionMethodCall.Program.Method1 () in C:\projects\exceptionmethodcall\exceptionmethodcall\program.cs:line 33
At ExceptionMethodCall.Program.Main (string[] args) in c:\Projects\ExceptionMethodCall\ExceptionMethodCall\ Program.cs:line 16

Delete try: Catch, we get the StackTrace as follows:

At ExceptionMethodCall.Program.Method1_1 () in C:\projects\exceptionmethodcall\exceptionmethodcall\program.cs:line 40
At ExceptionMethodCall.Program.Method1 () in C:\projects\exceptionmethodcall\exceptionmethodcall\program.cs:line 29
At ExceptionMethodCall.Program.Main (string[] args) in c:\Projects\ExceptionMethodCall\ExceptionMethodCall\ Program.cs:line 16

The results are obvious.

More:

If the METHOD1 code is written like this:

[CSharp]View PlainCopyprint?
  1. Public static int Method1 ()
  2. {
  3. Try
  4. {
  5. return method1_1 ();
  6. }
  7. catch (Exception ex)
  8. {
  9. Console.WriteLine (ex. Message);
  10. throw;
  11. }
  12. }

What's going to happen?

Note: This time there is no throw ex, but throw.

We found that StackTrace was not lost:

At ExceptionMethodCall.Program.Method1_1 () in C:\projects\exceptionmethodcall\exceptionmethodcall\program.cs:line 40
At ExceptionMethodCall.Program.Method1 () in C:\projects\exceptionmethodcall\exceptionmethodcall\program.cs:line 29
At ExceptionMethodCall.Program.Main (string[] args) in c:\Projects\ExceptionMethodCall\ExceptionMethodCall\ Program.cs:line 16

What is the reason?

I'll just say what I think, you can think it is OK, but I am not responsible for OH:

catch (Exception ex) instantiates a Exception object, which is the Exception you catch here. You can do it all, write log or print out,

But you just don't throw it. You throw it, and the exception stack is emptied. Without the throw, all exceptions are always saved in the exception stack.

In more:

If Method1 writes like this:

[CSharp]View PlainCopy print?
    1. Public static int Method1 ()
    2. {
    3. return method1_1 ();
    4. }

Note: Do not write any try: Catch. Some people may think that they will throw stacktrace, in fact not. Here Method1 () call Method1_1 (), it will be fully saved StackTrace.

In fact, it doesn't matter if you write more layers, stacktrace is still intact.

So in summary:

1. Do not add any try to the method if it is not necessary. Catch: The exception is processed only at the outermost layer of the calling method;

2. If you need to deal with Exception in the method, you can directly catch (Exception ex) processing, do not throw ex;

C #-catch (Exception ex) will throw away StackTrace what's going on?

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.