try catch finally 與continue的使用


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 子類與父類的相互轉換
{
    class Program
    {
        static void Main(string[] args)
        {
            //try catch finally 與 continue
            //如果在try中遇到continue,則忽略try中continue之后的語句
            //但是依然執行finally中語句
            //finally之外的語句也不執行
            bool _flag = true;
            while(true)
            {
                try
                {
                    if(_flag)
                        continue;
                    
                    //如果_falg為true,這下面的兩句不執行
                    Person per = new Student();
                    per.Say();//此時輸出father
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    //如果try中執行了continue,則這兩句依然要執行
                    Console.WriteLine("finally");
                    Console.ReadKey();
                }

                //如果在try中執行continue,則下面的兩條語句並不執行
                Console.WriteLine();
                Console.ReadKey();
            }
            
        }
    }

    class Person
    {
        public void Say()
        {
            Console.WriteLine("father");
        }
        
    }

    class Teacher:Person
    {
        public void Say()
        {
            Console.WriteLine("Teacher");
        }
    }

    class Student:Person
    {
        public void Say()
        {
            Console.WriteLine("Student");
        }
    }
     
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM