title: How can I avoid poor program performance from a TRY statement?
body: <p>I am currently developing a basic voxel (cuboid) game engine, and I have encountered a performance issue. Every frame the game:</p>
  
  <p>Finds player's viewing direction → Checks every co-ordinate in the player's direct view path → Finds closest occupied space → Draws a wireframe around this block.</p>
  
  <p>I am using OpenTK, a binding of OpenGL for .NET/Mono (specifically C#). My code OnRenderFrame contains this:</p>
  
  <pre><code>try
  {
      for (int i = 0; i &lt; 8; i++)
      {
          Vector3 block = InterfaceUtils.FindBlockLoc(Utilities.RoundToInt(cam.Position + (cam.lookat * i)));
          if (block != Vector3.Zero)
          {
              // Draw the wireframe
          }
      }
  }
  catch
  {
  }
  </code></pre>
  
  <p>The purpose of this code is to check up to eight blocks from the player until it finds a block. The only problem with this is that the try statement most often throws a (handled) exception: but the lag caused by this is massive. When I look into space, I only get 11FPS, but I get 50 if the code is successful.</p>
  
  <p><img src="https://i.stack.imgur.com/fdBp4.png" alt=""></p>
  
  <p>If the code is successful (it can draw the wireframe), I get this:</p>
  
  <p><img src="https://i.stack.imgur.com/KNfcO.png" alt=""></p>
  
  <p>The block-finding works by checking the objects list for a block in that location.</p>
  
  <pre><code>public static Vector3 FindBlockLoc(Vector3 location)
  {
      Vector3 result = new Vector3(Vector3.Zero);
  
      result = Deltashot.Game.objects.Find(v =&gt; v.Position == location).Position;
      if (result != null)
      {
          return result;
      }
      else
      {
          return Vector3.Zero;
      }
  }
  </code></pre>
  
  <p>However, this returns a NullReferenceException and I'm forced to use a TRY statement to get it to run. Which is back to square one again...</p>