Presentation is loading. Please wait.

Presentation is loading. Please wait.

Effective C# 50 Specific Ways to Improve Your C# Item 44 ~ 45 Kevin2012/9/26 1.

Similar presentations


Presentation on theme: "Effective C# 50 Specific Ways to Improve Your C# Item 44 ~ 45 Kevin2012/9/26 1."— Presentation transcript:

1 Effective C# 50 Specific Ways to Improve Your C# Item 44 ~ 45 Kevin2012/9/26 1

2 Agenda Item 44: Create Complete Application-Specific Exception Classes Item 44: Create Complete Application-Specific Exception Classes Chapter 6: Miscellaneous Chapter 6: Miscellaneous Item 45: Prefer the Strong Exception Guarantee Item 45: Prefer the Strong Exception Guarantee

3 Create Complete Application-Specific Exception Classes Item 44:

4 When and why?

5 Two Disclaimers Exceptions are not for every error condition you encounter Exceptions are not for every error condition you encounter – That cause long-lasting problems if they are not handled or reported immedia Writing a throw statement does not mean it's time to create a new exception class Writing a throw statement does not mean it's time to create a new exception class

6 New Exception Contains four constructors Contains four constructors – Different situations call for the different methods of constructing exceptions

7 Delegate the work to the base class implementation

8 Provide your own library's information This technique is called exception translation This technique is called exception translation – translating a low-level exception into a more high-level exception that provides more context about the error

9 Summary That’s must important for make library That’s must important for make library

10 Miscellaneous Chapter 6:

11 Miscellaneous Some items don't fit convenient categories. But that does not limit their importance. Understanding code access security is important for everyone, as is understanding exception-handling strategies. Other recommendations are constantly changing because C# is a living language, with an active community and an evolving standard. It pays to look forward to these changes and prepare for them; they will impact your future work. Some items don't fit convenient categories. But that does not limit their importance. Understanding code access security is important for everyone, as is understanding exception-handling strategies. Other recommendations are constantly changing because C# is a living language, with an active community and an evolving standard. It pays to look forward to these changes and prepare for them; they will impact your future work.

12 Prefer the Strong Exception Guarantee Item 45:

13 Exception-safe guarantees Dave Abrahams defined three exception-safe guarantees Dave Abrahams defined three exception-safe guarantees – The basic guarantee – The strong guarantee – The no-throw guarantee

14 What’s Basic Guarantee No resources are leaked and all objects are in a valid state after your application throws an exception. No resources are leaked and all objects are in a valid state after your application throws an exception.

15 What’s Strong Exception Guarantee Builds on the basic guarantee Builds on the basic guarantee If an operation terminates because of an exception, program state remains unchanged. Either an operation completes or it does not modify program state, there is no middle ground. If an operation terminates because of an exception, program state remains unchanged. Either an operation completes or it does not modify program state, there is no middle ground. The advantage of the strong guarantee is that you can more easily continue execution after catching an exception when the strong guarantee is followed. The advantage of the strong guarantee is that you can more easily continue execution after catching an exception when the strong guarantee is followed.

16 Rules The general guideline is to perform any data modifications in the following manner: The general guideline is to perform any data modifications in the following manner: – Make defensive copies of data that will be modified. – Perform any modifications to these defensive copies of the data. This includes any operations that might throw an exception. – Swap the temporary copies back to the original. This operation cannot throw an exception.

17 Defensive copy Example: Example:

18 BUT… Sometimes, the strong guarantee is just too inefficient to support… Sometimes, the strong guarantee is just too inefficient to support… – Simplest case : looping constructs Sometimes you cannot support the strong guarantee without introducing subtle bugs... Sometimes you cannot support the strong guarantee without introducing subtle bugs...

19 Consider this example The swap trick does not work for reference types it works only for value types. (see Item 23).Item 23

20 Use the Merge method

21 No-throw guarantee This just isn't practical for all routines in large programs This just isn't practical for all routines in large programs In a few locations, methods must enforce the no- throw guarantee In a few locations, methods must enforce the no- throw guarantee – Finalizes and Dispose methods must not throw exceptions The last location for the no-throw guarantee The last location for the no-throw guarantee – Delegate targets

22 Summary Watch carefully when swapping reference types; it can introduce numerous subtle bugs. Watch carefully when swapping reference types; it can introduce numerous subtle bugs.


Download ppt "Effective C# 50 Specific Ways to Improve Your C# Item 44 ~ 45 Kevin2012/9/26 1."

Similar presentations


Ads by Google