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?
- 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 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?
- Public static int Method1 ()
- {
- Try
- {
- return method1_1 ();
- }
- catch (Exception ex)
- {
- Console.WriteLine (ex. Message);
- throw;
- }
- }
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?
- Public static int Method1 ()
- {
- return method1_1 ();
- }
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?