If you??™d like to see what happens
in a case where an exception is thrown, simply uncomment the attempt to access the Nothing
reference inside the DoSomeWork method.
You may have noticed that we haven??™t addressed what happens if Rollback() throws
an exception. Clearly, for robust code, it??™s optimal to require that whatever operations
RollbackHelper performs in the process of a rollback be guaranteed never to throw. This
goes back to one of the most basic requirements for generating strong exception-safe and
exception-neutral code: you must have a well-defined set of operations that are guaranteed
not to throw in order for you to create robust exception-safe code.
A Using block is expanded into a Try/Finally block under the covers. And, when an
exception is thrown within a Finally block that is executing as the result of a previous exception,
that previous exception is simply lost and the new exception gets thrown. What??™s worse is
that the Finally block that was executing never gets to finish.
CHAPTER 8 n EXCEPTION HANDLING 164
As losing exception information makes it difficult to find problems, it is recommended
that you never throw an exception inside a Finally block. We??™ve mentioned this before in this
chapter, but it??™s so important it deserves being echoed.
Pages:
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280