Throw exception from getter : throw : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » throw »

 

Throw exception from getter








    
 
using System;

public class MyValue {
    public String Name;
}

class CardDeck {
    private MyValue[] Cards = new MyValue[52];

    public MyValue GetCard(int idx) {
        if ((idx >= 0) && (idx <= 51))
            return Cards[idx];
        else
            throw new IndexOutOfRangeException("Invalid Card");
    }

    public static void Main(String[] args) {
        try {
            CardDeck PokerDeck = new CardDeck();
            MyValue HiddenAce = PokerDeck.GetCard(53);
        } catch (IndexOutOfRangeException e) {
            Console.WriteLine(e.Message);
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        } finally {
            // Cleanup code
        }
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» throw