0

As part of a task for my first programming assignment we were required to create a program which tells the user how old they are in days, I've completed that however we need to use a try/catch. We've never been taught anything about them just basic concepts like classes/variables/arrays. Here is my code I need to add something in to simply tell the user if the DateTime object DateOfBirth is invalid in a text box and to start again, currently an invalid DOB just crashes the program. Here is is the code which calculates everything.

private void buttonDaysOld_Click(object sender, EventArgs e)
{
    DateTime DateOfBirth = new DateTime((int)comboBoxYear.SelectedItem, comboBoxMonth.SelectedIndex + 1, (int)comboBoxDay.SelectedItem);
    TimeSpan diff = DateTime.Now - DateOfBirth;
    int AgeInDays = (int)diff.TotalDays;
    MessageBox.Show(textBoxName.Text+" you are "+(AgeInDays.ToString())+ " Days Old");
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
user7724005
  • 17
  • 1
  • 6

2 Answers2

2

I'm guessing the error you're getting that crashes your application is a InvalidCastException when you try to cast your combobox values to a int. So all you need to do is surround your code with a try catch and do what you need in the catch part.

try {

    DateTime DateOfBirth = new DateTime((int)comboBoxYear.SelectedItem, comboBoxMonth.SelectedIndex + 1, (int)comboBoxDay.SelectedItem);
    TimeSpan diff = DateTime.Now - DateOfBirth;
    int AgeInDays = (int)diff.TotalDays;
    MessageBox.Show(textBoxName.Text + " you are " + (AgeInDays.ToString()) + " Days Old");
}
catch (InvalidCastException)
{
    MessageBox.Show("Wrong Format");
}

Note you should probably check the exact error you're getting and place it in the catch(your_error).

You can always put:

catch (Exception)

Which would catch all exception but it is not recommended that you do that, there's a post about that topic here.

If you're getting multiple exceptions that you need to handle, there's a great question about that, that explain how to handle it. You can learn more on try-catch and how they work by reading the documentation here.

Community
  • 1
  • 1
I.B
  • 2,925
  • 1
  • 9
  • 22
0

If you want date of birth validation in your program, code should look soweway like that:

private void buttonDaysOld_Click(object sender, EventArgs e)
{
    DateTime DateOfBirth;
    try
    {
        DateOfBirth = new DateTime((int)comboBoxYear.SelectedItem, comboBoxMonth.SelectedIndex + 1, (int)comboBoxDay.SelectedItem);
    }
    catch (Exception exception)
    {
        MessageBox.Show(exception.Message);
        return;
    }
    TimeSpan diff = DateTime.Now - DateOfBirth;
    int AgeInDays = (int)diff.TotalDays;
    MessageBox.Show(textBoxName.Text + " you are " + (AgeInDays.ToString()) + " Days Old");
}

If you want to become a qualified software developer, I suggest you:

1) get acquaintance with Exception mechanism, it's really convenient way to handle errors (exceptional situations) in your application: https://msdn.microsoft.com/en-us/library/ms173160.aspx

2) You can use special control – DatePicker – for choosing DateTime values.