软件编程
位置:首页>> 软件编程>> C#编程>> 轻松学习C#的异常处理

轻松学习C#的异常处理

作者:lijiao  发布时间:2022-09-14 22:10:20 

标签:C#,异常处理

异常是程序运行中发生的错误,异常处理是程序设计的一部分。错误的出现并不总是编写应用程序者的原因,有时候应用程序会因为终端用户的操作发生错误。无论如何,在编写程序前,都应该预测应用程序和代码中出现的错误。一般良好的编程规范也会避免一些不必要的程序错误的出现。

在项目的开发过程中,并不是所有的代码执行都和想象那样理想,总是避免不了异常的发生。这就需要编程语言的去处理这些异常,C#语言中有三种异常处理语句:

try...catch;//处理异常
try...finally;//清楚异常
try...catch...finally;//处理所有异常

一、用try...catch语句捕获异常

在try语句中包含容易产生异常的代码,接着捕获异常,catch段里的代码会注意进行适当的处理,
格式为:   

try
        {
        }
        catch(异常类  异常对象实例)
        {
        }

例一:用上述的语句捕获访问整型数组nums时产生索引越界异常,并提示给用户:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text
{
class Program
{
 static void Main(string[] args)
 {
  int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  try//捕获异常
  {
   for (int i = 0; i <= nums.Length; i++)//遍历数组所有元素
   {
    Console.Write(nums[i] + " ");
   }
  }
  catch (Exception a)//访问异常对象
  {
   Console.Write(a.Message);//输出异常错误
  }
   Console.WriteLine();
   Console.ReadLine();
 }

}
}

输出的结果为:

 轻松学习C#的异常处理

由于数据元素的索引是从0开始的,for语句遍历数组元素时,用了&ldquo;小于或等于&rdquo;,正好多遍历一次,所以出现索引越界。

二、清除与处理所有异常

如果用户对产生的错误不进行处理,而清除产生的错误分配的资源,那么可以使用try...finally语句来完成,这里的finally块用于清除try块中分配的任何资源以及运行任何即使在发生异常时也必须执行的带代码。格式为:

try
        {
        }
        catch(异常类  异常对象实例)
        {
        }
        finally
        {
        }

这个组合是处理所有异常最好的,它合并前面两种错误处理技术,即捕获错误,清除并继续执行应用程序。

例二:用240去除这个数组中的各元素,由于数组中的元素值有0,所以会产生处数据为0的错误。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text
{
class Program
{
 static void Main(string[] args)
 {
  int[] nums = { 4,8,12,0,10 };
  try//捕获异常
  {
   for (int i = 0; i < nums.Length; i++)
   {
    int valude = 0;
    valude = 240 / nums[i];
    Console.WriteLine("240/{0}={1}", nums[i], valude);
   }
  }
  catch (Exception a)//访问异常对象
  {
   Console.WriteLine(a.Message);//输出异常错误
  }
  finally
  {
   Console.WriteLine("有没有异常我都会运行");
  }
   Console.WriteLine();
   Console.ReadLine();
 }

}
}

输出的结果为:

轻松学习C#的异常处理

三、引发异常

在编写程序时,有时可能要引发异常,以便捕获异常。引发异常是通过throw语句和一个适当的异常类来实现的。其格式为:

throw  new  异常类(异常描述);

异常类可以是C#语言类库中提供的异常类,也可以是自定义异常类。异常描述为可选择项,用来描述产生异常错误,可产生异常时捕获到以便快速找到产生错误的代码。

例三:将字符串转换为整数的异常


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text
{
class Program
{
 static void Main(string[] args)
 {
  string str = "string";
  try
  {
   int returnInt;
   returnInt = Program.ConvertStringToInt(str);//调用转换
   Console.Write(returnInt);
  }
  catch (FormatException a)
  {
   Console.WriteLine(a.Message);
  }
  Console.ReadLine();
 }
 private static int ConvertStringToInt(string str)//定义转换函数
 {
  int intNum = 0;
  try
  {
   intNum = Convert.ToInt32(str);
   return intNum;
  }
  catch
  {
   throw new FormatException("转换错误");//引发异常
  }
 }

}
}

输出的结果为:

轻松学习C#的异常处理

四、自定义异常类

C#语言虽然预定义了许多异常类,但是,在有些场合,创建自己的异常类可能会方便。自定义异常类是通过继承System.Exception类来创建自己的异常类。其步骤是:

(1)声明一个异常,格式如下:class  异常类名:Exception{ }
(2)引发自己的异常,格式如下: throw(ExceptionName);

 例四:定义一个异常类MyException,然后引发这个异常类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text
{
class MyException : SystemException { }//声明异常
class Program
{
 static void Main(string[] args)
 {
  try
  {
   Console.WriteLine("引发异常前我是被执行的");//引发异常前的提示
   throw new MyException();
   Console.WriteLine("因为已经引发异常,所以我不能被执行");
  }
  catch (MyException)
  {
   Console.WriteLine("引发异常");
  }  
  Console.ReadLine();
 }

}
}

输出的结果为:

轻松学习C#的异常处理

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com