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

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
    }
}
 












本文转自terryli51CTO博客,原文链接: http://blog.51cto.com/terryli/519510,如需转载请自行联系原作者



上一篇:阿里云物联网平台计费问题处理


下一篇:MonoRail学习笔记十七:TransformFilter的使用