SimpleExceptions.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:1k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. namespace Wrox.ProCSharp.AdvancedCSharp
  3. {
  4.    public class MainEntryPoint
  5.    {
  6.       public static void Main()
  7.       {
  8.          string userInput;
  9.          while ( true )
  10.          {
  11.             try
  12.             {
  13.                Console.Write("Input a number between 0 and 5 " + 
  14.                   "(or just hit return to exit)> ");
  15.                userInput = Console.ReadLine();
  16.                if (userInput == "")
  17.                   break;
  18.                int index = Convert.ToInt32(userInput);
  19.                if (index < 0 || index > 5)
  20.                   throw new IndexOutOfRangeException(
  21.                      "You typed in " + userInput);
  22.                 Console.WriteLine("Your number was " + index);
  23.             }
  24.             catch (IndexOutOfRangeException e)
  25.             {
  26.                Console.WriteLine("Exception: " + 
  27.                   "Number should be between 0 and 5. " + e.Message);
  28.             }            
  29.             catch (Exception e)
  30.             {
  31.                Console.WriteLine(
  32.                   "An exception was thrown. Message was: " + e.Message);
  33.             }
  34.             catch
  35.             {
  36.                Console.WriteLine("Some other exception has occurred");
  37.             }
  38.             finally
  39.             {
  40.                Console.WriteLine("Thank you");
  41.             }
  42.          }
  43.       }
  44.    }
  45. }