what if I would have something like this:
public static void DoSomething()
{
try
{
//some code
try
{
//some other code
}
catch (Exception e)
{
log.Error("At the end something is wrong: " + e);
FunctionA(); //same function as in the first exception
}
}
catch (Exception e)
{
log.Error("At the start something wrong: " + e);
FunctionA();
}
}
So I have one try catch in another. The exceptions should be different and I want to handel them with the logger different. But let say I would like to call the same function for both of the exceptions. I have to write FunctionA() 2 times. Is this alright? Or is there any other problem with this type of exception? Suggestions?
You can use single try block with multiple catch blocks; and also you can use finally statements to execute some code section whether there is an exception or not.
Using multiple try-catch blocks inside a function is generally not a good idea. Also, it is better to handle an exception at the placed it occured.