Содержание
- 2. Agenda What are Exceptions? Handling Exceptions The System.Exception Class Types of Exceptions and their Hierarchy Raising
- 3. What are Exceptions? The exceptions in .NET Framework are classic implementation of the OOP exception model
- 4. Handling Exceptions In C# the exceptions can be handled by the try-catch-finally construction catch blocks can
- 5. Handling Exceptions – Example static void Main() { string s = Console.ReadLine(); try { Int32.Parse(s); Console.WriteLine(
- 6. The System.Exception Class Exception is a base class for all exceptions Important properties: Message – user-oriented
- 7. Exception Properties – Example class ExceptionsExample { public static void CauseFormatException() { string s = "an
- 8. Exception Properties The Message property gives brief description of the problem The StackTrace property is extremely
- 9. Exception Properties (2) File names and line numbers are accessible only if the compilation was in
- 10. Exception Hierarchy Exceptions in .NET Framework are organized in a hierarchy
- 11. Types of Exceptions .NET exceptions inherit from System.Exception The system exceptions inherit from System.SystemException, e.g. System.ArgumentException
- 12. Handling Exceptions When catching an exception of a particular class, all its inheritors (child exceptions) are
- 13. Find the Mistake! static void Main() { string s = Console.ReadLine(); try { Int32.Parse(s); } catch
- 14. Handling All Exceptions All exceptions thrown by .NET managed code inherit the System.Exception exception Unmanaged code
- 15. Throwing Exceptions Exceptions are thrown (raised) by throw keyword in C# Used to notify the calling
- 16. How Exceptions Work? Main() Method 1 Method 2 Method N 2. Method call 3. Method call
- 17. Using throw Keyword Throwing an exception with an error message: Exceptions can accept message and cause:
- 18. Re-Throwing Exceptions Caught exceptions can be re-thrown again: try { Int32.Parse(str); } catch (FormatException fe) {
- 19. Throwing Exceptions – Example public static double Sqrt(double value) { if (value throw new System.ArgumentOutOfRangeException( "Sqrt
- 20. Choosing the Exception Type When an invalid parameter is passed to a method: ArgumentException, ArgumentNullException, ArgumentOutOfRangeException
- 21. The try-finally Statement The statement: Ensures execution of given block in all cases When exception is
- 22. try-finally – Example static void TestTryFinally() { Console.WriteLine("Code executed before try-finally."); try { string str =
- 23. Exceptions – Best Practices catch blocks should begin with the exceptions lowest in the hierarchy And
- 24. Exceptions – Best Practices (2) When raising an exception always pass to the constructor good explanation
- 25. Exceptions – Best Practices (3) Exceptions can decrease the application performance Throw exceptions only in situations
- 26. Summary Exceptions provide flexible error handling mechanism in .NET Framework Allow errors to be handled at
- 28. Скачать презентацию