• C#_异常处理


       异常出现:

               异常出现的时间是在程序运行时。程序运行的时候的错误那么就需要进行异常处理。

       所有异常都是从Exception中继承的-----《重点

       //-----不多说了,直接上代码------------------------------------------

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace CSharp_Script
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args) 
    12         {
    13             //异常出现的时间是在程序运行时。程序运行的时候的错误那么就需要进行异常处理。
    14             //所有异常都是从Exception中继承的-----重点
    15 
    16             Console.WriteLine("请输入一个数字");
    17             //var data = Console.ReadLine();
    18             //int Num = Convert.ToInt32(Console.ReadLine()); //这里如果输入数字不会出现异常,可以如果输入其他字符则必然会出现异常。
    19 
    20             try   //检测可能出现的异常,有try 就必然有catch
    21             {
    22                 int Num = Convert.ToInt32(Console.ReadLine());
    23             }
    24             catch(Exception ex)  //运行时异常就必然会执行的一步
    25             {
    26                 Console.WriteLine("c程序出现了异常,异常信息:" + ex.Message);
    27             }
    28             finally //不管是否异常 程序都会执行这一步
    29             {
    30                 Console.WriteLine("不管是否异常,程序都会执行他。");
    31             }
    32 
    33 
    34         }
    35     }
    36 }

          把鼠标放在函数上时会出现异常警告 , 如:

     

        出现这种警告的时候注意最好添加    异常处理 。

    //------------------------------------------------------------------------------------------------------

       自定义异常处理类<>

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace CSharp_Script
     8 {
     9 
    10     //自定义的异常 。
    11     public class MyException : Exception 
    12     {
    13         private DateTime dt;
    14         public DateTime Dt
    15         {
    16             get { return dt; }
    17             set { dt = value; }
    18         }
    19 
    20         private string codeNum;  
    21 
    22         public string CodeNum
    23         {
    24             get { return codeNum; }
    25             set { codeNum = value; }
    26         }
    27 
    28 
    29         public MyException(string _codeNum ,Exception _ex)
    30             :base(_ex.Message)
    31         {
    32             codeNum = _codeNum;
    33             dt = DateTime.Now;
    34         }
    35 
    36         public void ShowExceptionMessage()
    37         {
    38             Console.WriteLine("异常出现的时间 :" + this.Dt + "异常出现的代码 :" + this.codeNum + "异常出现的信息 :" + this.Message);
    39         }
    40 
    41 
    42 
    43     }
    44 
    45 
    46 
    47 
    48 
    49 
    50     class Program
    51     {
    52         static void Main(string[] args) 
    53         {
    54             //异常出现的时间是在程序运行时。程序运行的时候的错误那么就需要进行异常处理。
    55             //所有异常都是从Exception中继承的-----重点
    56 
    57             Console.WriteLine("请输入一个数字");
    58             //var data = Console.ReadLine();
    59             //int Num = Convert.ToInt32(Console.ReadLine()); 
    60 
    61             //try   //检测可能出现的异常,有try 就必然有catch
    62             //{
    63             //    int Num = Convert.ToInt32(Console.ReadLine());
    64             //}
    65             //catch(Exception ex)  //运行时异常就必然会执行的一步
    66             //{
    67             //    Console.WriteLine("c程序出现了异常,异常信息:" + ex.Message);
    68             //}
    69             //finally //不管是否异常 程序都会执行这一步
    70             //{
    71             //    Console.WriteLine("不管是否异常,程序都会执行他。");
    72             //}
    73 
    74             try //检测异常
    75             {
    76                 int Num = Convert.ToInt32(Console.ReadLine());//这里如果输入数字不会出现异常,可以如果输入其他字符则必然会出现异常。
    77             }
    78             catch (Exception ex)   
    79             {
    80                 MyException myException = new MyException("001", ex); //实例化自定义的异常
    81                 myException.ShowExceptionMessage();  //异常处理
    82             }
    83             //finally
    84             //{
    85             //    Console.WriteLine("不管是否异常,程序都会执行他。");
    86             //}
    87 
    88 
    89             Console.ReadKey();
    90 
    91         }
    92     }
    93 }

     运行结果:

     

  • 相关阅读:
    一文详解ORB-SLAM3
    模块、进程、线程回调函数的逆向
    EPT的开启与处理
    VT MSR、CR、 Exception、#PF
    VT MTF VM-Exit
    VT技术对于除零异常的拦截与派发到3号中断
    异常与中断
    KVM_SET_SREGS 64位设置错误
    SQL注入实验学习笔记
    Pwn之简单patch
  • 原文地址:https://www.cnblogs.com/Roz-001/p/11127418.html
Copyright © 2020-2023  润新知