1

I have an application which is failing some process without much information getting logged. I'm looking at the code in question - which produces the error string I see logged - and I see the following structure:

public void MyFunction(MyObj param)
{
    try 
    {
        Do(param);
        Something(param);
        Useful(param);
    }
    catch (Exception)
    {
        throw new Exception("Something has gone wrong");
    }
}

My question is about the catch block. As you can see, it catches the exception which contains information about what is actually going wrong, and then throws a new exception with no reference whatsoever to the caught exception, so the real cause of the problem is lost. This new exception is thrown and eventually gets logged, hence the lack of information for me to go on in logs.

Is this just a matter of poor exception handling or is there any good reason to do this?

Luke
  • 2,434
  • 9
  • 39
  • 64
  • 3
    Does [this question](https://stackoverflow.com/questions/881473/why-catch-and-rethrow-an-exception-in-c) help you? The only good reason to write code like the above would be if you would want to hide the information from the inner exception. (Which obviously you dont whant in your situation.) – Felix Jun 22 '23 at 09:50
  • 1
    This is bad coding and you should replace it. – Zohar Peled Jun 22 '23 at 10:09

0 Answers0