-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathListing05.28.GeneralCatchBlocks.cs
More file actions
57 lines (52 loc) · 1.43 KB
/
Listing05.28.GeneralCatchBlocks.cs
File metadata and controls
57 lines (52 loc) · 1.43 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
47
48
49
50
51
52
53
54
55
56
57
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_28;
#pragma warning disable CS0168 // Variable is declared but never used
#region INCLUDE
// A previous catch clause already catches all exceptions
#pragma warning disable CS1058
#region EXCLUDE
using System;
public class ExceptionHandling
{
public static int Main()
{
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()!;
#endregion EXCLUDE
try
{
age = int.Parse(ageText);
Console.WriteLine(
$"Hi { firstName }! You are { age * 12 } months old.");
}
catch(FormatException exception)
{
Console.WriteLine(
$"The age entered ,{ageText}, is not valid.");
result = 1;
}
catch(Exception exception)
{
Console.WriteLine(
$"Unexpected error: { exception.Message }");
result = 1;
}
catch
{
Console.WriteLine("Unexpected error!");
result = 1;
}
finally
{
Console.WriteLine($"Goodbye { firstName }");
}
#endregion INCLUDE
return result;
}
}