using System;
public class ThrowTest
{
static void Main()
{
string s = null;
try
{
if (s == null)
{
throw new ArgumentNullException();
}
}
catch
{
s = "litao";
Console.WriteLine(s);
}
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请按任意键继续 . . .
}
}
//输出:
//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
}
}
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
}
}
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
}
}
本文转自terryli51CTO博客,原文链接: http://blog.51cto.com/terryli/519510,如需转载请自行联系原作者