【c#】try...catch...finally

来源:互联网 发布:yy摇骰子软件 编辑:程序博客网 时间:2024/05/29 12:31

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

namespace unit7
{
    class Program
    {
        enum Orientation : byte
        {
            North = 1,
            South = 2,
            East = 3,
            West = 4

        }
        static void Main(string[] args)
        {
            //Debug.WriteLine("debug");
            //Trace.WriteLine("trace");

            //Console.WriteLine("hello world");

            //Console.ReadKey();

            //练习4
            Orientation myDirection;

            for (byte myByte = 2; myByte < 10; myByte++)
            {
                try
                {
                    myDirection = checked((Orientation)myByte);

                    if (myByte < 1 || myByte > 10)
                    {
                        throw new ArgumentOutOfRangeException("myByte", myByte, "Value must between 1 and                                               4");  
                    }
                }
                catch(ArgumentOutOfRangeException e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Assigning default value, Orientation.North");
                    myDirection = Orientation.North;
                }
                finally
                {
                    Console.WriteLine("finally");
                }
                Console.WriteLine(myDirection);
            }
            Console.ReadKey();
        }
    }
}
原创粉丝点击