Try-Catch unable to locate the correct abnormal position, I recommend two effective techniques

The first cosmic development IDE Visual Studio debugger is very powerful, usually working debug help us solve many problems. Share exception trapping two tips today, hope to help solve some of the problems.
The following two cases, I believe we will encounter.

  • 1. Try-Catch statement is not used, when an abnormality occurs, it is possible to automatically jump to the point where the exception of, when using a Try-Catch catch exceptions, the Catch jump directly to the position and does not automatically locate the position error code.
  • 2. Try-Catch when the multilayer method call, and can not be viewed directly to a position error code.

Tip 1: exception code position automatically positioned

For question 1, the result is that we want the most, where there is an error code appears, and on where to locate directly, the abnormality on which lines of code, I could see at a glance, so that we can deal with the problem more quickly .
In this case Question 1, that appears, simply reproduce what abnormal a null reference

namespace ExceptionSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Random random = null;
                Console.WriteLine(random.Next());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadLine();
        }
     }
}     

The above exception code NullReferrenceException, Debug mode will jump to catch statement herein. You might think this is very simple ...... can actually practice, in a way you just only this one object?
Here Insert Picture Description
In practical work may be more than a random target, code complexity, multi-target enough, there are dozens, it's difficult to locate the error code is abnormal. StackTrace can navigate to that function call was wrong, and can not locate where a line of code wrong.
To resolve this behavior through the Debug "window" Visual Studio menu bar in abnormal settings to configure. As shown below:
Here Insert Picture Description
check on the Common Language Runtime Exceptions single box the following exceptions. A little more, before setting some changes.
Now we look at the code before using the Try-Catch statement to catch the exception and they will locate directly to the location of the error code, the following illustration:

       static void Main(string[] args)
        {
            try
            {
                Random random = null;
                Random random1 = new Random();
                Random random2 = new Random();
                Random random3 = new Random();
                Console.WriteLine(random1.Next());
                Console.WriteLine(random2.Next());
                Console.WriteLine(random3.Next());
                Console.WriteLine(random.Next());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadLine();
        }

Here Insert Picture Description

Tip 2: normal throw posture

还是之前的一个方法,我已经将异常设置回复默认了。

        static void Main(string[] args)
        {
            try
            {
                Random random = null;
                Console.WriteLine(random.Next());
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex);
                throw ex;
            }
        }

我们再输出中可以看到(ps:项目名称用的之前的,不介意哈)
Here Insert Picture Description
错误的代码在16行。可实际工作中的情况并不是这样简单,基本上是A方法调用B方法,B方法调用C方法,代码如下所示:
在Main方法中调用ThrowNullReferrence(),方法ThrowNullReferrence中调用SetNullReferrence()。代码变复杂后,一层嵌套一层。这个时候能正确显示出代码异常的位置吗?

        static void Main(string[] args)
        {
            try
            {
                ThrowNullReferrence();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex);
                throw ex;
            }
        }
        public  static void ThrowNullReferrence()
        {
            try
            {
                SetNullReferrence();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex);
                throw ex;
            }
        }
        public static void SetNullReferrence()
        {
            try {
                Random random = null;
                Console.WriteLine(random.Next());
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.Write(ex);
                throw ex;
            }
        }

我们可以通过下图看到:
Here Insert Picture Description
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 ExceptionSample.Program.SetNullReferrence() 位置 D:\Learn\延迟加载\LinqLayzLoad\LinqLayzLoad\Program.cs:行号 39System.NullReferenceException: 未将对象引用设置到对象的实例。
在 ExceptionSample.Program.SetNullReferrence() 位置 D:\Learn\延迟加载\LinqLayzLoad\LinqLayzLoad\Program.cs:行号 44
在 ExceptionSample.Program.ThrowNullReferrence() 位置 D:\Learn\延迟加载\LinqLayzLoad\LinqLayzLoad\Program.cs:行号 27System.NullReferenceException: 未将对象引用设置到对象的实例。
在 ExceptionSample.Program.ThrowNullReferrence() 位置 D:\Learn\延迟加载\LinqLayzLoad\LinqLayzLoad\Program.cs:行号 32
在 ExceptionSample.Program.Main(String[] args) 位置 D:\Learn\延迟加载\LinqLayzLoad\LinqLayzLoad\Program.cs:行号 15

Places where the error code in line 39, is throw more unusual location.
Causes it?
After the capture catch, if you want to throw up, you should re-instantiate a new exception object, and then throw up the outermost layer method to catch the exception is complete, of course, include the complete stack information, so as to positioning the position error code.
Throw new Exception To use
the example in FIG After transformation, precise positioning to
a null reference line 39 is abnormal
Console.WriteLine (random.Next ());
Here Insert Picture Description

Epilogue

Share an old rule of thumb programmer seen before: "The more coding, less debug"

Guess you like

Origin www.cnblogs.com/zhangmumu/p/12007159.html