0

I have this code where I am copying file from one location to another -

try
{
    var destination = Path.Combine(targetPath, fileInfo.Name);
    fileInfo.CopyTo(destination); //error thrown at this line
}
catch (Exception ex)
{
    //log error
}

because the destination path was already having file with same name, it threw the exception.
But to my surprise, the exception object was null.

I have fixed the issue by using CopyTo overload method to overwrite the existing file -

fileInfo.CopyTo(destination, true); //overwrite if file exists

which has fixed the issue.

But, my mind still is wondering - why exception object was null?
Any insights please.

stuartd
  • 62,136
  • 13
  • 120
  • 150
inutan
  • 9,740
  • 25
  • 74
  • 119
  • 3
    I'm guessing you set a breakpoint _on_ the catch line? The exception variable will be shown as null in the debugger until you step into the catch block. – gnud Mar 26 '20 at 22:23
  • Oh yes, you are right. Thanks for your help. I think, I was half asleep while debugging the issue. – inutan Mar 30 '20 at 12:13

0 Answers0