-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathListing05.26.CatchingAnException.cs
More file actions
46 lines (41 loc) · 1.11 KB
/
Listing05.26.CatchingAnException.cs
File metadata and controls
46 lines (41 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_26;
#region INCLUDE
public class ExceptionHandling
{
public static int Main(string[] args)
{
string? firstName;
string ageText;
int age;
int result = 0;
Console.Write("Enter your first name: ");
firstName = Console.ReadLine();
Console.Write("Enter your age: ");
// Assume not null for clarity
ageText = Console.ReadLine()!;
try
{
age = int.Parse(ageText);
Console.WriteLine(
$"Hi { firstName }! You are { age * 12 } months old.");
}
catch(FormatException)
{
Console.WriteLine(
$"The age entered, { ageText }, is not valid.");
result = 1;
}
catch(Exception exception)
{
Console.WriteLine(
$"Unexpected error: { exception.Message }");
result = 1;
}
finally
{
Console.WriteLine($"Goodbye { firstName }");
}
return result;
}
}
#endregion INCLUDE