当前位置:首页 > 代码相关 > 正文内容

C# try-catch处理异常

admin5年前 (2020-03-23)代码相关3158

使用try-catch进行异常处理,下面是两个小例子:

两个例子中没有写finally语句

finally的作用是无论有无异常,finally下的语句都会执行。

  1. //简单的处理异常
  1. namespace _20200323
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             string a = Console.ReadLine();
  8.             string b = Console.ReadLine();
  9.             Calculator cal = new Calculator();
  10.             int ad = cal.Add(a, b);
  11.             Console.WriteLine(ad);
  12.  
  13.         }
  14.  
  15.     }
  16.  
  17.     class Calculator
  18.     {
  19.         public int Add(string argu1, string argu2)
  20.         {
  21.             int a = 0;
  22.             int b = 0;
  23.             try
  24.             {
  25.                 a = int.Parse(argu1);
  26.                 b = int.Parse(argu2);
  27.             }
  28.  
  29.             catch
  30.             {
  31.                 Console.WriteLine("输入数据有误");
  32.             }
  33.  
  34.             int result = a + b;
  35.             return result;
  36.  
  37.         }
  38.     }
  39. }
  1. //精细的处理异常
  1. namespace _20200323
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             string a = Console.ReadLine();
  8.             string b = Console.ReadLine();
  9.             Calculator cal = new Calculator();
  10.             int ad = cal.Add(a, b);
  11.             Console.WriteLine(ad);
  12.  
  13.         }
  14.  
  15.     }
  16.  
  17.     class Calculator
  18.     {
  19.         public int Add(string argu1, string argu2)
  20.         {
  21.             int a = 0;
  22.             int b = 0;
  23.             try
  24.             {
  25.                 a = int.Parse(argu1);
  26.                 b = int.Parse(argu2);
  27.             }
  28.  
  29.             catch(ArgumentException)
  30.             {
  31.                 Console.WriteLine("输入数据为null");
  32.             }
  33.             catch(FormatException)
  34.             {
  35.                 Console.WriteLine("输入的不是数字");
  36.             }
  37.             catch(OverflowException)
  38.             {
  39.                 Console.WriteLine("超出值的范围");
  40.             }
  41.  
  42.             int result = a + b;
  43.             return result;
  44.  
  45.         }
  46.     }
  47. }

扫描二维码推送至手机访问。

版权声明:本文由lovedm.club发布,如需转载请注明出处。

本文链接:https://lovedm.club/?id=20

标签: C#

“C# try-catch处理异常” 的相关文章

递归计算1到x的和

递归真是个神奇的东西,当时学C的时候就没搞明白,学C#又遇到例子了。        public int SumXTo1(int x)     &n...

C#事件_Sample_2

事件的拥有者与事件的响应者是分开的情况+=是事件订阅操作符,左边是事件,右边是事件处理器。using System; using System.Windows.Forms; /// <summary> /// 事件的拥有者和事件的响应者是...

C# Lambda表达式

简单用法,一句一句来,便于理解 Func<int, int, int> func = new Func<int, int, int>((int a, int b) => { return a * b; });(int a, int b) => { ret...

C# 正则表达式(2)

// pattan = @"[^ahou]"; 表示匹配除ahou之外的字符,^在表示反义 string res4 = Regex.Replace(s, @"[^ahou]",&...

C# 与文件相关的几个类(2)

Directory类:静态类,主要处理文件目录。方法:CreateDirectory(String)在指定路径中创建所有目录和子目录,除非它们已经存在。返回值是一个DirectoryInfo对象Delete(String)从指定路径删除空目录。无返回值。Exists(String)确定给定路径是否引...

偶然想到的一个问题。。。

偶然想到的一个问题。。。

今天突然想C#中,用数组中的Max()方法和直接通过比较获取最大值的时间谁快,于是试了试:       static void Main(string[] args)   &nb...