title: Who should catch exception, the caller or the callee
body: <p>I'm currently studying delegates and read this page from codeproject <a href="http://www.codeproject.com/Articles/85296/important-uses-of-Delegates-and-Events" rel="nofollow">6 important uses of Delegates and Events</a>. I tried to implement the logic of the code in this class.</p>
  
  <pre><code>class MathOp
  {
      public delegate int OperationsDelegate(int x, int y);
      private OperationsDelegate Del;
  
      public OperationsDelegate GetDelegate()
      {
          Console.WriteLine("\n\tEnter 1 for Addition\n\tEnter 2 for Subtraction\n\tEnter 3 for Multiplication\n\tEnter 4 for Division");
          int input;
          try
          {
              input = Convert.ToInt32(Console.ReadLine());
              switch(input)
              {
                  case 1:
                      Del = Add;
                      break;
                  case 2:
                      Del = Sub;
                      break;
                  case 3:
                      Del = Mul;
                      break;
                  case 4:
                      Del = Div;
                      break;
              }
  
  
          }
          catch(FormatException error) { Console.WriteLine(error.Message);  }
          catch(OverflowException error) { Console.WriteLine(error.Message); }
          catch(NullReferenceException error) { Console.WriteLine(error.Message); }
  
          return Del;
      }
  
  
      private int Add(int a, int b) { return a + b; }
      private int Sub(int a, int b) { return a - b; }
      private int Mul(int a, int b) { return a * b; }
      private int Div(int a, int b) { return a / b; }            
  }
  </code></pre>
  
  <p>Then I called the method from the Main method like:</p>
  
  <pre><code> MathOp Cal = new MathOp();
  
  
          try
          {
              int answer = Cal.GetDelegate().Invoke(2, 4);
              Console.WriteLine(answer);
          }
          catch(FormatException er) { Console.WriteLine(er.Message); }   
          catch(NullReferenceException er) { Console.WriteLine(er.Message); }  
  </code></pre>
  
  <ol>
  <li><p>I only added the <code>try</code>/<code>catch</code> in the <code>Main</code> method, because my exception inside the <code>GetDelegate</code> was not doing anything. I mean if I step through my code, they are being caught, BUT the program still crashed!!</p></li>
  <li><p>If I go ahead and compile this, I get two errors for entering a letter instead of number. I am expecting only a <code>FormatException</code>, but I get both <code>FormatException</code> and <code>NullReferenceException</code>!</p>
  
  <p>I thought it is the invoke method but it's not</p></li>
  <li><p>Generally and beyond this code, Am I right in thinking that the burden of catching an exception falls upon the callee method and not the caller?!</p></li>
  </ol>