mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
[CSharp/en]Added exception handling
This commit is contained in:
parent
dc34af2b5d
commit
710f9fa03b
@ -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
|
Func<int, int> square = (x) => x * x; // Last T item is the return value
|
||||||
Console.WriteLine(square(3)); // 9
|
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.
|
// DISPOSABLE RESOURCES MANAGEMENT - let you handle unmanaged resources easily.
|
||||||
// Most of objects that access unmanaged resources (file handle, device contexts, etc.)
|
// Most of objects that access unmanaged resources (file handle, device contexts, etc.)
|
||||||
// implement the IDisposable interface. The using statement takes care of
|
// implement the IDisposable interface. The using statement takes care of
|
||||||
@ -780,10 +805,17 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
}
|
}
|
||||||
set
|
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()
|
public override string Info()
|
||||||
{
|
{
|
||||||
string result = "PennyFarthing bicycle ";
|
string result = "PennyFarthing bicycle ";
|
||||||
@ -842,7 +874,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
## Topics Not Covered
|
## Topics Not Covered
|
||||||
|
|
||||||
* Attributes
|
* Attributes
|
||||||
* Exceptions, Abstraction
|
* Abstraction
|
||||||
* ASP.NET (Web Forms/MVC/WebMatrix)
|
* ASP.NET (Web Forms/MVC/WebMatrix)
|
||||||
* Winforms
|
* Winforms
|
||||||
* Windows Presentation Foundation (WPF)
|
* Windows Presentation Foundation (WPF)
|
||||||
|
Loading…
Reference in New Issue
Block a user