zoukankan      html  css  js  c++  java
  • 错误调式 异常处理

    第一种:

      通过Console.WriteLine()进行一步步输出调试。

    第二种:

      通过错误列表的反馈进行更改。双击错误的提示可以直接调转到错误的那一行(VS2015是这样的,其他的VS软件也应该是这样子)。

    第三种:

      通过写日志的方式。

      Trace.WriteLine()、

      Debug.WriteLine():该命令只能在调试模式下执行。

    第四种:断点调试。

      在VS2015中,在代码的文本编辑的最左边的灰色竖条对应行代码的位置点击一下即可以添加添加断电,按F11可以单步调试,此外还有F10也可以用,其他的暂没有发现。

    Trace.WriteLine()和Debug.WriteLine()的说明

      Debug.WriteLine()只能在调式模式下使用并不表示"Debug.WriteLine()次于Trace.WriteLine()",如果真要这个说那只是对于所有版本的信息才有效。

      我们输出调式信息常常只在调式的时候希望输出调试信息,所以这个时候选择Debug.WriteLine(),而且Debug.WriteLine()只在调试模式下使用,所以Debug.WriteLine()不能编译成发布文件,所以使用发布文件更小了。

    异常处理

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 异常处理主要有try/catch/throw/finally组成
                // 注意:C#不想python那样能够使用try{}catch{}else{}finally{},C#没有这种错误异常处理方式
                try
                {
                    // 这是尝试是否能够正确执行的代码块
                }
                catch
                {
                    // 如果try模块出现错误,那么就会执行catch这一块
                    throw;  // throw是用来抛出错误
                }
                finally
                {
                    // 不管上面try或者catch那一块在执行都会执行这一块
                }
            }
        }
    }
    #define myName
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                // C#中的异常类
                // C#中的异常类主要是直接或者间接的派生于System.Exception类。System.ApplicationException和System.SystemException类是派生于System.Exception类的异常类
                // Systemp.IO.IOException:处理I/O错误。System.IndexOutOfRangeException:处理当方法指向超出范围的数组索引时生成的错误。System.ArrayTypeMismatchException:处理当数组类型不匹配时生成的错误
                DivNumbers d = new DivNumbers();
                d.division(25, 0);
                
            }
            class DivNumbers
            {
                int result;
                public DivNumbers()
                {
                    result = 0;
                }
                public void division(int num1, int num2)
                {
                    try
                    {
                        result = num1 / num2;  // 尝试执行try
                    }
                    catch (DivideByZeroException e)
                    {
                        Console.WriteLine("Exception:{0}", e.Message);  // 显示错误原因
                    }
                    finally
                    {
                        Console.WriteLine("Result:{0}", result);  // 最终显示的结果
                    }
                }
            }
        }
    }

    用户还可以自己定义异常类:

    #define myName
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                // ExcepTest test1 = new ExcepTest(0);  // 会报错
                ExcepTest test1 = new ExcepTest(1);  // 正常显示
                test1.showExcep();
                
            }
            public class TempException : ApplicationException
            {
                public TempException(string message) : base(message) { }
            }
            public class ExcepTest
            {
                private int number1;
                public ExcepTest(int num)
                {
                    this.number1 = num;
                }
                public void showExcep()
                {
                    if (this.number1 == 0)
                    {
                        throw (new TempException("不能输入0"));  // 抛出一个错误
                    }
                    else
                    {
                        Console.WriteLine("吼吼吼吼吼吼...");
                    }
                }
            }
        }
    }
  • 相关阅读:
    POJ 2195 Going Home (费用流)
    POJ 1087 A Plug for UNIX (网络流,最大流)
    凸包的直径——旋转卡壳
    凸包--Graham扫描法
    POJ 3167 Layout(差分约束)
    POJ 2187 Beauty Contest(凸包,旋转卡壳)
    HDU 1392 Surround the Trees(凸包)
    HDU 3416 Marriage Match IV(最短路,网络流)
    【USACO4.2】草地排水Drainage Ditches(最大流)
    【模板】网络最大流
  • 原文地址:https://www.cnblogs.com/namejr/p/10337584.html
Copyright © 2011-2022 走看看