Consider the following snippet:
using System; using System.Collections.Generic; using System.Linq; using System.Net; namespace ForumLogins { public class VBULLETIN { HttpWebRequest request; public void ForumLogins(string url) { request = (HttpWebRequest)WebRequest.Create(url); } } }
According to MSDN the ‘Create’ method can return 4 exceptions. Now I cant dissolve those exceptions.. because well otherwise the class wont function. But what should I do? Should I still wrap it in a try/catch block? and in the catch ‘throw a new exception’.. or let the person who implments this class handle the exceptions?
Cuz this is a constructor .. and I guess constructors arnt really supposed to fail?
If your constructor can fail, then you should probably let the person who implements the class handle the errors. Just be sure to document the fact that it might fail so they know to try/catch.
On a related note, constructors should never fail. =)
Instead, put the logic that can possibly fail into a ‘Go()’ method. That way you can instantiate a real class, but put a try/catch around segments of the code, and still reference the object outside of the try-catch block later. Just my $.02.