diff options
author | angelsl <angelsl@users.noreply.github.com> | 2017-02-10 00:40:04 +0800 |
---|---|---|
committer | ven <vendethiel@hotmail.fr> | 2017-02-09 17:40:04 +0100 |
commit | eb1ed1729b9d9539e16e17590cfb911cd72a279e (patch) | |
tree | dbf2cf9436e5f59472ca939f50e8a98b75d6994d | |
parent | b4ca1f76e2d5b955b011ea8d9b38212634fb1777 (diff) |
[csharp/en] Add exception filters, pragma directives (#2409)
* [csharp/en] Add exception filters, pragma directives
* Remove redundant catch
-rw-r--r-- | csharp.html.markdown | 92 |
1 files changed, 89 insertions, 3 deletions
diff --git a/csharp.html.markdown b/csharp.html.markdown index 442700a6..cca99fb0 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -1006,15 +1006,101 @@ on a new line! ""Wow!"", the masses cried"; // x?.y will return null immediately if x is null; y is not evaluated => GenieName?.ToUpper(); } + + static class MagicService + { + private static bool LogException(Exception ex) + { + /* log exception somewhere */ + return false; + } + + public static bool CastSpell(string spell) + { + try + { + // Pretend we call API here + throw new MagicServiceException("Spell failed", 42); + + // Spell succeeded + return true; + } + // Only catch if Code is 42 i.e. spell failed + catch(MagicServiceException ex) when (ex.Code == 42) + { + // Spell failed + return false; + } + // Other exceptions, or MagicServiceException where Code is not 42 + catch(Exception ex) when (LogException(ex)) + { + // Execution never reaches this block + // The stack is not unwound + } + return false; + // Note that catching a MagicServiceException and rethrowing if Code + // is not 42 or 117 is different, as then the final catch-all block + // will not catch the rethrown exception + } + } + + public class MagicServiceException : Exception + { + public int Code { get; } + + public MagicServiceException(string message, int code) : base(message) + { + Code = code; + } + } + + public static class PragmaWarning { + // Obsolete attribute + [Obsolete("Use NewMethod instead", false)] + public static void ObsoleteMethod() + { + /* obsolete code */ + } + + public static void NewMethod() + { + /* new code */ + } + + public static void Main() + { + ObsoleteMethod(); // CS0618: 'ObsoleteMethod is obsolete: Use NewMethod instead' +#pragma warning disable CS0618 + ObsoleteMethod(); // no warning +#pragma warning restore CS0618 + ObsoleteMethod(); // CS0618: 'ObsoleteMethod is obsolete: Use NewMethod instead' + } + } } // End Namespace + +using System; +// C# 6, static using +using static System.Math; + +namespace Learning.More.CSharp +{ + class StaticUsing + { + static void Main() + { + // Without a static using statement.. + Console.WriteLine("The square root of 4 is {}.", Math.Sqrt(4)); + // With one + Console.WriteLine("The square root of 4 is {}.", Sqrt(4)); + } + } +} ``` ## Topics Not Covered * Attributes - * async/await, pragma directives - * Exception filters - * `using static` + * async/await * Web Development * ASP.NET MVC & WebApi (new) * ASP.NET Web Forms (old) |