The difference between "throw" keyword and the usage of code written between "catch" curly brackets

Viewed 111
  1. Generally, when can we use the " throw " keyword?
  2. I cannot differentiate between the usage of code written between "catch" curly brackets and the usage of "throw".

Can anyone explain to me using the following example?

using System;

namespace ConsoleApp1
{ 
    class RethrowDemo
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Trying in main() method");
                MethodA();
            }
            catch(Exception ae)
            {
                Console.WriteLine("caught in main() method-- \n{0}",ae.Message);
            }

            Console.WriteLine("Main() method is done");
        }

        private static void MethodA()
        {
            try
            {
                Console.WriteLine("Trying in Method A");
                MethodB();
            }
            catch(Exception)
            {
                Console.WriteLine("Caught in method A");
                throw;
            }
        }

        private static void MethodB()
        {
            try
            {
                Console.WriteLine("Trying in Method B");
                MethodC();
            }
            catch (Exception)
            {
                Console.WriteLine("Caught in method B");
                throw;
            }
        }

        private static void MethodC()
        {
            Console.WriteLine("In Method C");
            throw (new Exception("This came from method C"));    
        }
    }
}
3 Answers

The throw keyword is used when you want to throw an exception manually. Check this for details. You use the try-catch block when you are usually working with files or connecting to a host, etc. When your code can run into an exception like file does not exist or similar and you put the code inside the catch {} which will be executed if an exception is thrown. Check this

I will add my answer here similar to what I wrote in the comment section, thank you to 500 - Internal Server Error (great username btw) for extra context.

The throw by itself keyword will retain the exception that was thrown before it, thus you retain the original stack trace. Using throw new will create a new exception which will get rid of the old stack trace unless you use the constructor that 500 mentioned.

As Zoltan mentioned, the usage of a try-catch block is used in situation such as file reading, connections, api calls, etc. where the catch block is there to prevent a hard system crash and allows you to do some work as well. Perhaps you want to propogate the error (thus the throw keyword) or you just want to swallow it and log it somewhere else.

That is, as far as I am aware so far, the biggest difference between throw and throw new

Consider the following code.

using System;
                    
public class Program
{
    public static void Main()
    {
        try{
            method2();
        }catch(Exception e){
            Console.WriteLine(e);
        }
    }
    
    static void method1(){
        throw new InvalidProgramException("Exception 1");
    }
    
    static void method2(){
        try{
            method1();
        }catch(InvalidProgramException e){
            Console.WriteLine("I am method2. I am bad");
            throw;
        }
    }
}

If you want to throw an exception manually, then you can use the throw keyword. Consider the method1. In method1 I have thrown an InvlidProgramException.

Now consider the method2. If we use only throw keyword then we call that rethrow. It will throw the error caught by the catch block.

If the catch block caught any exception, the code inside the catch block will be executed. In method2 when it catches the exception, first print I am method2. I am bad and then throw the error caught by catch, in this case, InvalidProgramException.

Since I have used Exception in the main method catch block, it can catch any type of exception. Otherwise catch can catch only the specified type of exception, like method2 catch block, can catch only InvalidProgramException.

Related