mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
[csharp/en] Add exception filters, pragma directives (#2409)
* [csharp/en] Add exception filters, pragma directives * Remove redundant catch
This commit is contained in:
parent
b4ca1f76e2
commit
eb1ed1729b
@ -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
|
// x?.y will return null immediately if x is null; y is not evaluated
|
||||||
=> GenieName?.ToUpper();
|
=> 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
|
} // 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
|
## Topics Not Covered
|
||||||
|
|
||||||
* Attributes
|
* Attributes
|
||||||
* async/await, pragma directives
|
* async/await
|
||||||
* Exception filters
|
|
||||||
* `using static`
|
|
||||||
* Web Development
|
* Web Development
|
||||||
* ASP.NET MVC & WebApi (new)
|
* ASP.NET MVC & WebApi (new)
|
||||||
* ASP.NET Web Forms (old)
|
* ASP.NET Web Forms (old)
|
||||||
|
Loading…
Reference in New Issue
Block a user