第一次在这里发帖,如果我的一些细节有误,非常抱歉。我试图捕捉用户输入无效(不存在)员工的异常。我尝试了许多不同的变体,但从未引发错误。相反,代码只是终止,在这种情况下,它将在以下行之后退出:
MyPostSalary = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
谁能看到我做错了什么?
完整代码:
Console.Write("Employee ID: ");
myEID = Console.ReadLine();
try
{
Console.Write("Post ID: ");
myPID = Console.ReadLine();
if ((myEmployees[myEID] is MonthlyPaidEmployee) || (myEmployees[myEID] isWeeklyPaidEmployee))
{
Console.Write("Post Name: ");
MyPostName = Console.ReadLine();
Console.Write("Post Start Date: ");
MyPostStartDate = Convert.ToDateTime(Console.ReadLine());
Console.Write("Post End Date: ");
MyPostEndDate = Convert.ToDateTime(Console.ReadLine());
Console.Write("Post Salary: ");
MyPostSalary = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
myPost = new Post(myPID, MyPostName, MyPostStartDate, MyPostEndDate, MyPostSalary);
if (myEmployees[myEID] is MonthlyPaidEmployee)
{
myMonthlyEmp = (MonthlyPaidEmployee)myEmployees[myEID];
myMonthlyEmp.PostHistory.Add(myPID, myPost);
}
if (myEmployees[myEID] is WeeklyPaidEmployee)
{
myWeeklyEmp = (WeeklyPaidEmployee)myEmployees[myEID];
myWeeklyEmp.WeeklyPaidEmployeePostHistory.Add(myPID, myPost);
}
}
}
catch (NullReferenceException ex)
{
Console.WriteLine("Employee ID does not exist.");
Console.WriteLine(ex.Message);
Console.WriteLine();
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}