try..catch..finally

来源:互联网 发布:守望先锋辅助软件 编辑:程序博客网 时间:2024/05/07 01:11
       try
        {           
            ProcessString(s);
        }
        catch (Exception e)
        {
            Console.WriteLine("{0} Exception caught.", e);
        }
        finally
        {
        }
============================================================
C#
    class TryFinallyTest{    static void ProcessString(string s)    {        if (s == null)        {            throw new ArgumentNullException();        }    }    static void Main()    {        string s = null; // For demonstration purposes.        try        {                        ProcessString(s);        }        catch (Exception e)        {            Console.WriteLine("{0} Exception caught.", e);        }    }}    /*    Output:    System.ArgumentNullException: Value cannot be null.       at TryFinallyTest.Main() Exception caught.     * */

=================================================================

class ThrowTest3{    static void ProcessString(string s)    {        if (s == null)        {            throw new ArgumentNullException();        }    }    static void Main()    {        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);        }    }}/* Output:System.ArgumentNullException: Value cannot be null.   at TryFinallyTest.Main() First exception caught.*/

原创粉丝点击