");UserInput=Console.ReadLine();if" />
文档库 最新最全的文档下载
当前位置:文档库 › 第4章例4.24(面向对象编程进阶练习)

第4章例4.24(面向对象编程进阶练习)

using System;
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)
// 抛出IndexOutOfRangeException异常
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");
}
}
}
}

相关文档
相关文档 最新文档