Thanks for contributing to this question , I decided to go ahead with making the Create () method throw exceptions , as Jon Skeet said you don’t need to handle them everywhere and just let them bubble up, seems like the best approach for larger applications.
So now I am instantiating my classes using this code:
try { SmartForms smartForms = SmartForms.Create("ball"); smartForms.Show(); } catch (CannotInstantiateException ex) { Console.WriteLine("Item could not be instantiated: {0}", ex.Message); }
custom exception:
using System; namespace TestFactory234.Exceptions { class CannotInstantiateException : Exception { } }
How do I know which exception class to use?
In the above instance, I created my own exception, because I don’t know where to get the list of “all system exceptions” or if it’s not yet “can not instantiate the object” or if it has some other value for using it, etc. . Choosing the type of exception always seems to me such an arbitrary process, so creating my own is apparently the best idea in general.
Or am I missing something about exceptions? What other implications for deciding what type of exception to use?
source share