您的位置:首页 > 其它

throw在try中抛出异常,然后用catch捕捉并处理这个异常,同时catch也可以再次抛出这个异常

2007-04-05 01:34 731 查看
using System;
public class ThrowTest
{
static void Main()
{
string s = null;
try
{
if (s == null)
{
throw new ArgumentNullException();
}
}
catch
{
s = "litao";
Console.WriteLine(s);
}
Console.Write("The string s is null"); // not executed
}
}
//输出:
//litao
//The string s is null请按任意键继续 . . .
同上:
// throw example
using System;
public class ThrowTest
{
static void Main()
{
string s = null;
try
{
if (s == null)
{
throw new ArgumentNullException();
}
}
catch
{
s = "litao";

Console.WriteLine(s);

throw ;//利用空throw语句,可以再次把已经捕获的异常抛出。
}

Console.Write("The string s is null"); // not executed
}
}

同上
// throw example
using System;
public class ThrowTest
{
static void Main()
{
string s = null;
try
{
if (s == null)
{
throw(new ArgumentNullException());
}
}
catch(ArgumentException exc)
{
s = "litao";

Console.WriteLine(s);
throw (exc); //等同throw exc;
//还等同 throw ;//利用空throw语句,可以再次把已经捕获的异常抛出。
//Console.WriteLine(exc.Message);
//Console.WriteLine(exc);
}

Console.Write("The string s is null"); // not executed
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  异常 休闲 throw try catch
相关文章推荐