Differences between "Throw","Throw ex" and "Throw new Exception(...)" in .NET

The Throw statement is used to inform about the occurrence of an exception when a program is executing.
Exceptions contain a property named StackTrace. This string contains the name of the methods on the current call stack, apart from the file name and line number from where the exception was thrown for each method.

There are 3 different ways to use the Throw statement:
  • Throw Ex:  The original stack trace will be overwritten with a new stack trace that starts from the throwing method.
  • Throw:  Preserves the original stack trace information because rethrows the original exception.
  • Throw new Exception(...): Keeps original stack trace and adds aditional details.
       Example:

class Maths
{
    static void Main(string[] args)
    {
        try
        {
            MethodX();
        }
        catch (Exception ex)
        {
            //Throw Ex - Method X stack trace (Method Y has been overridden)
            //Exception: System.DivideByZeroException: Attempted to divide by zero.
            //   at Maths.MethodX()
            //   at Maths.Main(String[] args)

            //Throw - Original exception stack with MethodY at the beginning of the stack trace
            //Exception: System.DivideByZeroException: Attempted to divide by zero.            

             //    at Maths.MethodY()
            //   at Maths.MethodX()
            //   at Maths.Main(String[] args)

            //Throw new Exception(...) - Original exception stack with MethodY at the beginning of the stack trace + additional details
            //Exception: System.Exception: Additional details... ---> System.DivideByZeroException: Attempted to divide by zero. 

            //   at Maths.MethodY()
            //   at Maths.MethodX()
            //   --- End of inner exception stack trace ---
            //   at Maths.MethodX()
            //   at Maths.Main(String[] args)

             Console.WriteLine("Exception: {0}", ex.ToString());
             Console.ReadLine();
        }
    }

    public static void MethodX()
    {
        try
        {
            MethodY();
        }
        catch (Exception ex)
        {
              throw ex;
              throw;
              throw new Exception("Additional details...", ex);
     
        }
    }
    public static void MethodY()
    {
         throw new System.DivideByZeroException();
    }
}

 Summary

The throw statement is used in different ways depending on the situation:
  • If you need to track an exception from the origin, use Throw .
  • If you need to add extra details to the exception or repackage it, always useThrow new exception(" ",ex).
Note: The program execution performance is only affected when exceptions are thrown. There is nothing to do with the quantity of try/catch blocks that you add in your code but with the number of exceptions thrown.




Comments

Popular posts from this blog

How to convert fields in lower case/upper case within Info path forms

How to auto save a browser enabled info path form every X minutes in Sharepoint 2007, 2010 and 2013

Auto-populate the person/Group picker from the current user on InfoPath 2007/2010/2013