Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I handle exceptions in factory pattern class or let them propagate upwards?
    primarykey
    data
    text
    <p>I am C# developer and I'd like to use factory pattern to create objects that represent file or directory on hard disk. FileInfo and DirectoryInfo are classes in .NET used for that purpose but instead of them I'd like to have my own IFileInfo and IDirectoryInfo interfaces that wraps them. The reason for using custom interfaces rather than built in classes is because I'd like to add some additional properties such as file shell icon etc...</p> <p>So I'd like to use factory pattern to create instances of IFileInfo and IDirectoryInfo for a given string of a file/directory path. </p> <p>So factory class would look something like:</p> <pre><code>public class MyFileInfoFactory { IFileInfo Create(string filePath) { System.IO.FileInfo file = new System.IO.FileInfo(filePath); // turn FileInfo to my IFileInfo etc... } } </code></pre> <p>Now looking at System.IO.FileInfo docs (<a href="http://msdn.microsoft.com/en-us/library/system.io.fileinfo.fileinfo.aspx" rel="nofollow">FileInfo MSDN</a>) I see there are several possible exceptions that can be thrown. My question is should I handle all these exceptions in the factory Create() method? Or should I let them propagate upwards to whichever code is calling MyFileInfoFactory.Create()?</p> <p>If first solution is way to go, then what would be the next step? For example should I return null or possibly throw some custom exception with InnerException property set to the actual exception thrown from new FileInfo() constructor?</p> <p>Just wondering what would be the best practice in this concrete scenario case... </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload