I am interested in finding out what is the correct way of implementing error handling for this situation in C#.
The system tries to do an operation. If the operation succeeded (returns a non-error code), the system logs in a log database that the operation was succesfull. If the operation returns a error code or something caused an exception before the operation, the system will log an error. However, if the an error is caught when logging, the system shouldn't log an error in the database.
I am using C#. The following code is what I have used until now, but I don't know if it is the best practice in this situation.
int response = -1; //some error code
try
{
//some code to prepare the operation - may cause exceptions
response = DoOperation();
//some code to clean after the operation - may cause exceptions
}
catch
{
//error handling
}
try
{
if (response > 0) //non-error code
//log event in database
else
//log event error in database
}
catch
{
//logging error handling
}
Do you have any suggestions for improving my code?
Note: The catch blocks include in the original code handling specific errors, I just used a general catch blocks for code simplicity in my question.