title: Exceptions in C# try catch throw
body: <p>I am handling exceptions in this program. Is this the correct way to handle exceptions?</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace Assignment1
  {
      class Calc
      {
          public static double n1;
          public static double n2;
          public static char op1;
          public static double result;
  
          public static void Calculate(String num1, String num2, String op)
          {
              op1 = Convert.ToChar(op);
              n1 = Convert.ToDouble(num1);
              n2 = Convert.ToDouble(num2);
  
              switch (op1)
              {
                  case '+':
                      Console.WriteLine(Add());
                      break;
  
                  case '-':
                      Console.WriteLine(Minus());
                      break;
  
                  case '*':
                      Console.WriteLine(Multiply());
                      break;
  
                  case '/':    // ******exception handling***** //
  
                      if (n2 == 0)
                      {
                          try
                          {
                              throw new Exception(&quot;number 2 cannot be zero&quot;);
                          }
                          catch (Exception ex)
                          {
                              Console.WriteLine(ex.Message);
                          }
                      }
  
                      Console.WriteLine(Divide());
                      break;
  
                  case '^':
                      Console.WriteLine(Power());
                      break;
  
                  default:    // ******exception handling***** //
                      try
                      {
                          throw new Exception(&quot;Invalid Operator: Exception Handling&quot;);
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine(e.Message);
                      }
                      break;
              }
          }
  
          public static double Add()
          {
              Console.WriteLine(&quot;Addition&quot;);
              result = n1 + n2;
              return result;
          }
  
          public static double Minus()
          {
              Console.WriteLine(&quot;Subtraction&quot;);
              result = n1 - n2;
              return result;
          }
  
          public static double Multiply()
          {
              Console.WriteLine(&quot;Multiplication&quot;);
              result = n1 * n2;
              return result;
          }
  
          public static double Divide()
          {
              Console.WriteLine(&quot;Division&quot;);
              result = n1 / n2;
              return result;
          }
  
          public static double Power()
          {
              Console.WriteLine(&quot;X power of n&quot;);
              result = Math.Pow(n1, n2);
              return result;
          }
      }
  }
  </code></pre>
  <p>Here I do not want to use the built-in divide by zero exception for division as it doesn't work for the <em>double</em> data type.</p>
  <p>I also want to get an exception when the user enters an invalid operator. So I have used an exception handler in the default case.</p>
  <p>I am taking the user input in the main function and then passing the inputs to calculate the function as parameters from <em>main</em>.</p>
  <p>Can I make any changes to make this program more efficient?</p>