轻松学习C#的异常处理
异常是程序运行中发生的错误,异常处理是程序设计的一部分。错误的出现并不总是编写应用程序者的原因,有时候应用程序会因为终端用户的操作发生错误。无论如何,在编写程序前,都应该预测应用程序和代码中出现的错误。一般良好的编程规范也会避免一些不必要的程序错误的出现。
在项目的开发过程中,并不是所有的代码执行都和想象那样理想,总是避免不了异常的发生。这就需要编程语言的去处理这些异常,c#语言中有三种异常处理语句:
try...catch;//处理异常
try...finally;//清楚异常
try...catch...finally;//处理所有异常
一、用try...catch语句捕获异常
在try语句中包含容易产生异常的代码,接着捕获异常,catch段里的代码会注意进行适当的处理,
格式为:
try
{
}
catch(异常类 异常对象实例)
{
}
例一:用上述的语句捕获访问整型数组nums时产生索引越界异常,并提示给用户:
<span style="font-size:18px;">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(); } } }</span>
输出的结果为:
由于数据元素的索引是从0开始的,for语句遍历数组元素时,用了“小于或等于”,正好多遍历一次,所以出现索引越界。
二、清除与处理所有异常
如果用户对产生的错误不进行处理,而清除产生的错误分配的资源,那么可以使用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(); } } }
输出的结果为:
三、引发异常
在编写程序时,有时可能要引发异常,以便捕获异常。引发异常是通过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#语言虽然预定义了许多异常类,但是,在有些场合,创建自己的异常类可能会方便。自定义异常类是通过继承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#给图片添加水印完整实例