Finished Solution, Throwing & Catching Exceptions, repo at 9f4de32

jason.zhu 2021-02-22 00:56:24 +00:00
parent 5ea0c1bd54
commit 15bd8fcbb5
2 changed files with 26 additions and 5 deletions

View File

@ -16,7 +16,13 @@ namespace GradeBook
public void AddGrade(double grade) public void AddGrade(double grade)
{ {
this.grades.Add(grade); if (grade <= 100 && grade >= 0)
{
this.grades.Add(grade);
} else
{
throw new ArgumentException($"Invalid {nameof(grade)}");
}
} }
public void AddLetterGrade(char letter) public void AddLetterGrade(char letter)

View File

@ -16,8 +16,7 @@ namespace GradeBook
var book = new Book("Jason's Grade Book"); var book = new Book("Jason's Grade Book");
string input; string input;
bool if_continue = true; while (true)
while (if_continue)
{ {
Console.WriteLine("Give input: "); Console.WriteLine("Give input: ");
input = Console.ReadLine(); input = Console.ReadLine();
@ -27,8 +26,24 @@ namespace GradeBook
break; break;
} else } else
{ {
Console.WriteLine($"Received: {input}"); try
book.AddGrade(Convert.ToDouble(input)); {
Console.WriteLine($"Received: {input}");
book.AddGrade(Convert.ToDouble(input));
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Will always run for all catch scenario
Console.WriteLine("Always Done");
}
} }
} }