diff options
author | Laoujin <woutervs@hotmail.com> | 2015-01-31 23:14:58 +0100 |
---|---|---|
committer | Laoujin <woutervs@hotmail.com> | 2015-01-31 23:14:58 +0100 |
commit | 710f9fa03b5cea3d60906edb977133e37a19ebf6 (patch) | |
tree | 8bd5120bd6c01d5b6f207c9931f8c6e34cd991b2 | |
parent | dc34af2b5de78fb7e7f04e982e145d0bd67a3728 (diff) |
[CSharp/en]Added exception handling
-rw-r--r-- | csharp.html.markdown | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/csharp.html.markdown b/csharp.html.markdown index 4258a0c6..df265d8c 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -462,6 +462,31 @@ on a new line! ""Wow!"", the masses cried"; Func<int, int> square = (x) => x * x; // Last T item is the return value Console.WriteLine(square(3)); // 9 + // ERROR HANDLING - coping with an uncertain world + try + { + var funBike = PennyFarthing.CreateWithGears(6); + + // will no longer execute because CreateWithGears throws an exception + string some = ""; + if (true) some = null; + some.ToLower(); // throws a NullReferenceException + } + catch (NotSupportedException) + { + Console.WriteLine("Not so much fun now!"); + } + catch (Exception ex) // catch all other exceptions + { + throw new ApplicationException("It hit the fan", ex); + // throw; // A rethrow that preserves the callstack + } + // catch { } // catch-all without capturing the Exception + finally + { + // executes after try or catch + } + // DISPOSABLE RESOURCES MANAGEMENT - let you handle unmanaged resources easily. // Most of objects that access unmanaged resources (file handle, device contexts, etc.) // implement the IDisposable interface. The using statement takes care of @@ -780,10 +805,17 @@ on a new line! ""Wow!"", the masses cried"; } set { - throw new ArgumentException("You can't change gears on a PennyFarthing"); + throw new InvalidOperationException("You can't change gears on a PennyFarthing"); } } + public static PennyFarthing CreateWithGears(int gears) + { + var penny = new PennyFarthing(1, 1); + penny.Gear = gears; // Oops, can't do this! + return penny; + } + public override string Info() { string result = "PennyFarthing bicycle "; @@ -842,7 +874,7 @@ on a new line! ""Wow!"", the masses cried"; ## Topics Not Covered * Attributes - * Exceptions, Abstraction + * Abstraction * ASP.NET (Web Forms/MVC/WebMatrix) * Winforms * Windows Presentation Foundation (WPF) |