b2c电子商务网站比较/武汉百度开户代理
编写C#异常处理代码时,应将特殊处理的异常写在前边,普通异常写在后边,不然代码不会被编译,特殊的异常永远不会执行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ExcepTion
{class Program{static void Main(string[] args){while (true){try{string userInput;Console.WriteLine("Input a number between 0 and 5 " + "( or just hit return to exit)>");userInput = Console.ReadLine();if (string.IsNullOrEmpty(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 ex){Console.WriteLine("Exception: " + $"Number should be between 0 and 5.{ex.Message}");}// 普通异常处理catch (Exception ex){Console.WriteLine($"An exception was throw. Message was:{ex.Message}");}finally{Console.WriteLine("Thank you\n");}}}}
}