mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-04 13:57:37 +00:00
Initial commit
This commit is contained in:
parent
ca7684985d
commit
49bdd4dda1
60 changed files with 1527 additions and 30 deletions
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
40
vs4win/Chapter03/HandlingExceptions/Program.cs
Normal file
40
vs4win/Chapter03/HandlingExceptions/Program.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
WriteLine("Before parsing");
|
||||
Write("What is your age? ");
|
||||
string? input = ReadLine(); // or use "49" in a notebook
|
||||
|
||||
try
|
||||
{
|
||||
int age = int.Parse(input!);
|
||||
WriteLine($"You are {age} years old.");
|
||||
}
|
||||
catch (OverflowException)
|
||||
{
|
||||
WriteLine("Your age is a valid number format but it is either too big or small.");
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
WriteLine("The age you entered is not a valid number format.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteLine($"{ex.GetType()} says {ex.Message}");
|
||||
}
|
||||
WriteLine("After parsing");
|
||||
|
||||
Write("Enter an amount: ");
|
||||
string amount = ReadLine()!;
|
||||
if (string.IsNullOrEmpty(amount)) return;
|
||||
|
||||
try
|
||||
{
|
||||
decimal amountValue = decimal.Parse(amount);
|
||||
WriteLine($"Amount formatted as currency: {amountValue:C}");
|
||||
}
|
||||
catch (FormatException) when (amount.Contains("$"))
|
||||
{
|
||||
WriteLine("Amounts cannot use the dollar sign!");
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
WriteLine("Amounts must only contain digits!");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue