The obvious one is that, no matter where in this method an exception is
thrown, the state of the EmployeeDatabase will remain unaffected. But, what if an exception is
thrown inside ListSwap()? Ah! Here you have the second trick: ListSwap() will never throw an
exception. One of the most important features required in order to create exception-neutral
code is that you have a small set of operations that are guaranteed not to fail under normal
circumstances. No, we??™re not considering the case of a catastrophic earthquake or tornado at
that point. Let??™s see why ListSwap() won??™t throw any exceptions.
In order to create exception-neutral code, it??™s imperative that you have a handful of
operations, such as an assignment operation, that are guaranteed not to throw. Thankfully, the
CLR provides such operations. The assignment of references, when no conversion is required,
is one example. Every reference to an object is stored in a location, and that location has a
type associated with it. However, once the locations exist, copying a reference from one to the
other is a simple memory copy to already allocated locations, and that cannot fail. That??™s great
for when you??™re copying references of one type to references of the same type.
Pages:
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251