string userInput;
while (true)
{
try
{
Console.Write("请输入一个0--5之间的数字 (或按回车键退出) >: ");
userInput = Console.ReadLine();
if (userInput == "")
break; // retrurn;
int index = Convert.ToInt32(userInput);
if (index < 0 || index > 5)
{
throw new IndexOutOfRangeException("但你输入的是:" + userInput);
}
Console.WriteLine("你输入的是:" + index);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("异常: 输入的数字必须在0--5之间," + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("发生一个异常:{0} " + ex.Message);
}
catch
{
Console.WriteLine("发生了其他的异常!");
}
finally
{
Console.WriteLine("谢谢你!");
Console.WriteLine();
}
}