(I've searched similar threads and couldn't find anything that addressed this particular issue, though there were several similar such as here and here.)
I'm evaluating performance of our app and I'm noticing that we're getting some IOExceptions "Cannot locate resource". I'm not sure exactly how many times it's happening (largely depends on how user uses the app), but it's at least a dozen or so.
I'm assuming that exceptions in general are performance expensive as are File I/O calls like File.Exists()
. I know that it's always good practice to check if a file exists before you try and load it. My question is, how much performance gain would I see if I check if this particular file existed? (Again, ignore the "you should do this anyway", I'm just trying to get an understanding of performance).
Option 1:
try
{
return (ResourceDictionary) Application.LoadComponent(uri);
}
catch (Exception)
{
//If it's not there, don't do anything
}
This doesn't make an extra IO call, but sometimes throws and exception which is swallowed.
Option 2:
if(File.Exists(uri))
{
return (ResourceDictionary) Application.LoadComponent(uri);
}