using System; namespace Wrox.ProCSharp.AdvancedCSharp { public class MainEntryPoint { public static void Main() { string userInput; while ( true ) { try { Console.Write("Input a number between 0 and 5 " + "(or just hit return to exit)> "); userInput = Console.ReadLine(); if (userInput == "") break; int index = Convert.ToInt32(userInput);//把字符串转换成整数类型. if (index < 0 || index > 5) throw new IndexOutOfRangeException(//抛出异常类型. "You typed in " + userInput); Console.WriteLine("Your number was " + index); } catch (IndexOutOfRangeException e) //扑捉到了异常 { Console.WriteLine("Exception: " + "Number should be between 0 and 5. " + e.Message); } catch (Exception e) //扑捉所有一般异常 { Console.WriteLine( "An exception was thrown. Message was: " + e.Message); } catch { Console.WriteLine("Some other exception has occurred"); } finally //无论有没有异常都执行的模块 { Console.WriteLine("Thank you"); } } } } }