- Generally, when can we use the " throw " keyword?
- 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"));
}
}
}