What is the best way for using Disposable objects, assuming Constructor and Process methods may throw exception? I generally prefer one of below implementations.
try-catch surrounding using block
try { using (Disposable dispObj = new Disposable()) { dispObj.Process(); } } catch (Exception ex) { // Do something }
try-catch-finally block.
Disposable dispObj2 = null; try { dispObj2 = new Disposable(); dispObj2.Process(); } catch (Exception ex) { // Do something } finally { if (dispObj2 != null) { dispObj2.Dispose(); } }
UPDATE:
Again: "assuming Constuctor and Process methods may throw exception". I really do not understand why did nobody care about exceptions in their answers.