【c#】try...catch...finally 素颜马尾好姑娘i 2022-06-12 02:13 185阅读 0赞 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(); \} \} \}
还没有评论,来说两句吧...