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

我的c#之路(8.异常处理语句)

2014-06-11 21:28 267 查看
异常处理语句

1.

--在应用程序遇到异常情况(如被零除情况或内存不足警告)时,就会产生异常。

--在可能引发异常的语句周围使用try块。

--try块发生异常后,控制流会立即跳转到关联的异常处理程序(如果存在)。

--如果给定异常没有异常处理程序,则程序将停止执行,并显示一条错误消息。

--如果catch块定义了一个异常变量,则可以使用它来获取有关所发生异常的类型的更多信息。

--可能导致异常的操作通过try关键字来执行。

--异常处理程序是在异常发生时执行的代码块,在C#中,catch关键字用于定义异常处理程序。

--程序可以使用throw关键字显式地引发异常。

--异常对象包含有关错误的详细信息,其中包括调用堆栈的状态以及有关错误的文本说明。

--即使引发了异常,finally块中的代码也会执行,从而使程序可以释放资源。

2.

throw

--用于发出在程序执行期间出现反常情况(异常)的信号

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

namespace ConsoleApplication1
{
public class ThrowTest
{
static void Main(string[] args)
{
string s = null;
if (s == null)
{
throw new ArgumentNullException();//使用throw语句引发异常
}
Console.Write("The string s is null");//not executed
}
}
}
3.
try-catch

--由一个try块后跟一个或多个catch子句构成,这些子句指定不同的异常处理程序

--try块包含可能导致异常的保护代码。该块一直执行到引发异常或成功完成为止

--catch子句使用时可以不带任何参数,这种情况下它捕获任何类型的异常

catch()

{

}

--catch子句可以接受从System.Exception派生的对象参数,这种情况下它处理特定的异常。

catch(InvalidCastException e)

{

}

--在同一个try-catch语句中可以使用一个以上的特定catch子句。这种情况下catch子句的顺序很重要,因为会按顺序检查catch子句。将先捕获特定程序较高的异常,而不是特定程度较小的异常。

catch(ArgumentNullException e)

{

Console.WriteLine("{0} First exception caught.",e);

}

catch(Exception e)

{

Console.WriteLine("{0} Second exception caught.",e);

}

--在catch块中可以使用throw语句再次引发已由catch语句捕获的异常

catch(InvalidCastException e)

{

throw(e);//Rethrowing exception e

}



catch

{

throw;

}

--在try块内部时应该只初始化其中声明的变量,否则,完成该块的执行前可能发生异常

int x;

try

{

x=123;//Error

}

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

namespace ConsoleApplication1
{
public class ThrowTest
{
static void Main(string[] args)
{
string s = null;
if (s == null)
{
throw new ArgumentNullException();//使用throw语句引发异常
}
Console.Write("The string s is null");//not executed
}
}
}

使用了两个catch语句。最先出现的最特定的异常被捕获
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class MainClass
{
static void ProcessString(string s)
{
if (s == null)
{
throw new ArgumentNullException();//若要捕获特定程度最小的异常,使用throw new Exception();
}
}
static void Main(string[] args)
{
try
{
string s = null;
ProcessString(s);
}
//Most specific:
catch (ArgumentNullException e)
{
Console.WriteLine("{0} First exception caught.",e);
}
//Least specific:
catch (Exception e)
{
Console.WriteLine("{0} Second exception caught.", e);
}
}
}
}4.
try-finally

--finally块用于清除try块中分配的任何资源,以及运行任何即使在发生异常时也必须执行的代码。控制总是传递给finally块,与try块的退出方式无关。(catch用于处理语句块中出现的异常,而finally用于保证代码语句块的执行,与前面的try块的退出方式无关)

有一个导致异常的无效转换语句。当运行程序时,您收到一条运行时错误信息,但finally子句仍继续执行并显示输出。

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

namespace ConsoleApplication1
{
class MainClass
{
static void Main(string[] args)
{
int i = 123;
string s = "Some string";
object o = s;
try
{
//Invalid conversion;o contains a string not an int
i = (int)o;
}
finally
{
Console.Write("i={0}",i);
}
}
}
}5.
try-catch-finally

--catch和finally一起使用的常见方式是:在try块中获取并使用资源,在catch块中处理异常情况,并在finally块中释放资源。

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

namespace ConsoleApplication1
{
class MainClass
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Executing the try statement.");
throw new NullReferenceException();
}
catch (NullReferenceException e)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
catch
{
Console.WriteLine("Caught exception #2.");
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 语言