1

First post here, so sorry if I've got some of the niceties wrong. I'm trying to catch an exception where the user enters an invalid (non-existent) employee. I've tried many different variations, but the error is never raised. Instead the code just terminates, in this case it will drop out after the following lines:

 MyPostSalary = Convert.ToDouble(Console.ReadLine());
 Console.WriteLine();

Can anyone see what I'm doing wrong?

Complete code:

       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();

        }
Peopleware
  • 1,399
  • 1
  • 25
  • 42
mojo
  • 59
  • 1
  • 1
  • 5
  • 4
    Your main problem is that you didn't show us the exception you're receiving or where you're receiving it. – John Saunders Dec 05 '12 at 14:40
  • 2
    @Daniel is right, moreover **please** do not catch an exception for an invalid user input. It's a pretty common situation (and absolutely **unexceptional**). Let's validate the input to reject it! – Adriano Repetti Dec 05 '12 at 14:42
  • @John Saunders. I don't receive any sort of exception. The code simply terminates. I'm expecting it to pick up that an Employee ID does not exist in my employees collection. I have tried the debugger and the code never seems to reach the point where it catches the error. I should say that I'm fairly new to c#! – mojo Dec 05 '12 at 14:56
  • Sounds like you're running it in release mode - in debug I think the exception is caught? – imekon Dec 05 '12 at 15:55
  • @Peopleware, I don't think it can be considered a duplicate. What he's asking for is the exact opposite, he can't catch the exception. See my answer below. – tedebus May 19 '21 at 10:17

5 Answers5

9

Are you in debug on Visual Studio (or your IDE)? If yes, it maybe you are catching the exception but the IDE is set to show a warning message when the exception raises... Try to run your application outside the IDE, if it works you can disable warning when NullReferenceExceptions raise , but it is quite dangerous. To do this look for "Exceptions settings" in Visual Studio (or something like this, I'm not sure because I use the italian version).

tedebus
  • 978
  • 13
  • 20
3

You are only catching NullReferenceException exception, but as you see in the method description of Convert.ToDouble(), it throws an InvalidCastException.

So try catch (InvalidCastException ex) instead.

Also pay attention to FormatException and OverflowException.

Peopleware
  • 1,399
  • 1
  • 25
  • 42
1

It's fairly clear that you are not getting a NullReferenceException. I recommend that you try the following:

catch (Exception ex)
{
        Console.WriteLine(ex.ToString());
        Console.WriteLine();
        Console.WriteLine("Press any key to continue");
        Console.ReadLine();        
}

Find out which exception you're receiving, and either catch that specific exception, or else prevent it from occurring in the first place.

You should never catch NullReferenceException.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

Aren't you getting a double conversion exception? You should probably use Double.TryConvert rather than Convert.

imekon
  • 1,501
  • 4
  • 22
  • 39
0

It looks like the entered text can't be converted to a double.

Try catching an InvalidCastException.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443