源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

轻松学习C#的异常处理

  • 时间:2021-11-29 01:42 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:轻松学习C#的异常处理
       异常是程序运行中发生的错误,异常处理是程序设计的一部分。错误的出现并不总是编写应用程序者的原因,有时候应用程序会因为终端用户的操作发生错误。无论如何,在编写程序前,都应该预测应用程序和代码中出现的错误。一般良好的编程规范也会避免一些不必要的程序错误的出现。         在项目的开发过程中,并不是所有的代码执行都和想象那样理想,总是避免不了异常的发生。这就需要编程语言的去处理这些异常,C#语言中有三种异常处理语句:       [b]  try...catch;//处理异常         try...finally;//清楚异常         try...catch...finally;//处理所有异常 [/b][b]一、用try...catch语句捕获异常[/b]         在try语句中包含容易产生异常的代码,接着捕获异常,catch段里的代码会注意进行适当的处理, 格式为:        [b] try         {         }         catch(异常类  异常对象实例)         {         } [/b][b]例一:用上述的语句捕获访问整型数组nums时产生索引越界异常,并提示给用户: [/b]
<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> 
输出的结果为:  [img]http://files.jb51.net/file_images/article/201511/20151127162010978.jpg?20151027162035[/img]        由于数据元素的索引是从0开始的,for语句遍历数组元素时,用了“小于或等于”,正好多遍历一次,所以出现索引越界。 [b]二、清除与处理所有异常[/b]         如果用户对产生的错误不进行处理,而清除产生的错误分配的资源,那么可以使用try...finally语句来完成,这里的finally块用于清除try块中分配的任何资源以及运行任何即使在发生异常时也必须执行的带代码。格式为:         [b] try         {         }         catch(异常类  异常对象实例)         {         }         finally         {         } [/b]        这个组合是处理所有异常最好的,它合并前面两种错误处理技术,即捕获错误,清除并继续执行应用程序。 [b]例二:用240去除这个数组中的各元素,由于数组中的元素值有0,所以会产生处数据为0的错误。 [/b]
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(); 
  } 
 
 } 
} 
        输出的结果为: [img]http://files.jb51.net/file_images/article/201511/20151127162107466.jpg?20151027162134[/img] [b]三、引发异常[/b]         在编写程序时,有时可能要引发异常,以便捕获异常。引发异常是通过throw语句和一个适当的异常类来实现的。其格式为:         [b]throw  new  异常类(异常描述); [/b]        异常类可以是C#语言类库中提供的异常类,也可以是自定义异常类。异常描述为可选择项,用来描述产生异常错误,可产生异常时捕获到以便快速找到产生错误的代码。 [b]例三:将字符串转换为整数的异常 [/b]
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("转换错误");//引发异常 
   } 
  } 
 
 } 
} 
输出的结果为: [img]http://files.jb51.net/file_images/article/201511/20151127162152572.jpg?2015102716222[/img] [b]四、自定义异常类[/b]           C#语言虽然预定义了许多异常类,但是,在有些场合,创建自己的异常类可能会方便。自定义异常类是通过继承System.Exception类来创建自己的异常类。其步骤是: (1)声明一个异常,格式如下:class  异常类名:Exception{ } (2)引发自己的异常,格式如下: throw(ExceptionName); [b] 例四:定义一个异常类MyException,然后引发这个异常类。[/b]
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(); 
  } 
 
 } 
} 
输出的结果为: [img]http://files.jb51.net/file_images/article/201511/20151127162234445.jpg?20151027162243[/img] 以上就是本文的全部内容,希望对大家的学习有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部