Suppose I have this application:
class Program
{
static void Main(string[] args)
{
try
{
throw new SomeSpecificException("testing");
}
catch (SomeSpecificException ex)
{
Console.WriteLine("Caught SomeSpecificException");
throw new Exception("testing");
}
catch (Exception ex)
{
Console.WriteLine("Caught Exception");
}
Console.ReadKey();
}
}
// just for StackOverflow demo purposes
internal class SomeSpecificException : Exception
{
public SomeSpecificException(string message) : base(message)
{ }
public SomeSpecificException()
{ }
}
And my required output is as follows:
Caught SomeSpecificException Caught Exception
Is there a way to do this? Or is my design totally off base?
Background:
I am adding code to an existing code base. The code base catches Exception (generalized exception) and does some logging, removes files, etc. But I have a unique behavior I'd like to only happen when SomeSpecificException
is thrown. Afterwards, I'd like the exception handling to pass to the existing Exception catch clause so that I do not have to modify too much of the existing code.
I am aware of checking for exception's type using reflection or some other runtime technique and putting an if statement in the Exception catching clause as per Catch Multiple Exceptions at Once but I wanted to get feedback on whether the above approach is possible.