title: showing argumentoutofrange from caller
body: <p>I would like to know if there is a way to get the "ArgumentOutOfRangeException" from the caller of a method/function:</p>
  
  <p>Been searching for this for days, and no luck...
  Either rare, or bad choice of search parameters...</p>
  
  <p>Example:</p>
  
  <pre><code>class Main
  {
      struct XYZ
      {
          public int X, Y, Z;
          public XYZ(int x, int y, int z)
          {
              X = x;
              Y = y;
              Z = z;
          }
      }
      class ArrayE&lt;T&gt;
      {
          public T[, ,] data;
  
          public ArrayE(XYZ xyz)
          {
              data = new T[xyz.X, xyz.Y, xyz.Z];
          }
          public T this[XYZ xyz]
          {
              get
              {
                  if (OutOfBounds(xyz))
                  {
                      throw new ArgumentOutOfRangeException(); // Error shows here in debug
                  }
                  else
                  {
                      return data[xyz.X, xyz.Y, xyz.Z];
                  }
              }
              set
              {
                  if (OutOfBounds(xyz))
                  {
                      throw new ArgumentOutOfRangeException(); // Error shows here in debug
                  }
                  else
                  { 
                      data[xyz.X, xyz.Y, xyz.Z] = value; 
                  }
              }
          }
  
          bool OutOfBounds(XYZ xyz)
          {
              return xyz.X &lt; 0 | xyz.Y &lt; 0 | xyz.Z &lt; 0 |
                         xyz.X &gt;= data.GetLength(0) |
                         xyz.Y &gt;= data.GetLength(1) |
                         xyz.Z &gt;= data.GetLength(2);
          }
      }
  
      ArrayE&lt;int&gt; Data;
      public void Test()
      {
          XYZ xyz = new XYZ(10, 10, 10);
          Data = new ArrayE&lt;int&gt;(xyz);
  
          xyz = new XYZ(1,0,2);
          Data[xyz] = 2;
  
  
          xyz = new XYZ(1, -1, 2);
          Data[xyz] = 4; // I would like the debugger to stop here
          //As if I did this:
          Data[xyz.X,xyz.Y,xyz.Z] = 4 // The debugger would then stop at this line
  
          //Example of what I want
          int[] array = new int[10];
          array[2] = 1;
          array[-1] = 3; // This is the behavior I would like to happen when out of bounds, The error shows here.
      }
  }
  </code></pre>
  
  <p><strong>Edit:</strong> More Specific:<br>
  When I start debuggin, I want the debugger to stop at the line "Data[xyz] = 4;" where xyz is a invalid index, so I can see where it was called from, and not from inside the Data[xyz] get set function.</p>
  
  <p>If a regular int[] would get an invalid index, for example -1, then the debugger would stop 
  at that line, and you can look at the lines before the array was accessed, so it would be possible to find out why the index is what it is.
  With the current setup, when an invalid index of xyz is used on the ArrayE[xyz], the debugger stops inside the get set, making me unable to track and figure out why the xyz is outside the bounds of the array...</p>
  
  <p>Hope that it is more clear, as to what I'm asking about.<br>
  I could use try catch, but is there no other way? doing the try catch would quickly make the code unreadable and unmanagable, since the program is very large...</p>
  
  <p>So how can it be done, if possible?</p>
  
  <p>I am using Visual C# 2010 Express</p>