//通过继承Exception类来实现自己定义异常
- public class CardOutOfRangeException:Exception{
- public Cards cards;
- public Cards Cards{
- get{return cards;}
- }
- public CardOutOfRangeException(Cards other):base("一副牌只有54张"){
- cards=other;
- }
- }
抛出自定义异常
- throw new CardOutOfRangeException(cards);
打印异常信息
- try{
- }catch(CardOutOfRangeException e){
- Console.WriteLine(e.Message);
- }