1. 程式人生 > 其它 >不帶引數的丟擲異常

不帶引數的丟擲異常

        public static void PrintArgs(string args)
        {
            try
            {
                try
                {
                    if (args == null)
                    {
                        ArgumentNullException myEx = new ArgumentNullException("args");
                        throw myEx;//這裡是丟擲異常然後直接執行到Catch中。當Catch沒有語句,則會中斷程式。
                    }
                    Console.WriteLine(args);
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("Message:{0}", e.Message);
                    throw;//不帶引數的丟擲異常。會跳到catch中區執行程式。
                }
            }
            catch 
            {
                Console.WriteLine("Other Catch:Handing an Exception");
            }
        }
    static void Main(string[] args)
    {
        string s = null;
        PrintArgs(s);           
        Console.ReadLine();
    }