您的位置:首页 > 编程语言 > C#

c#的异常

2014-03-10 13:16 417 查看
感觉跟c++没什么不同

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

namespace testUnNormal
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[15];
try
{
int a = 0;
int b = 50 / a;

/*
byte b = 10;
b = checked((byte)(b+2000));
*/
/*
* string str = "abcdefd+10";
* int i = -1;
* i = Int32.Parse(str);//这个会被 System.FormatException 捕获
*/

}
catch(System.FormatException ex)
{
Console.WriteLine(ex.Message);
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//始终运行
Console.WriteLine("hello" + arr.ToString());
}

Console.ReadLine();
}
}
}


System.Exception 异常的基类

异常包含两种类型

SystemException

ApplicationException



Message和InnerException比较重要

System.Exception类包含3个方法

GetBaseException()获取异常的根Exception对象
GetObjectData()获取与异常相关的SerializationInfo属性的值
GetType() 获取当前实例的运行时类型
常用的异常类

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: