0

I have two radio buttons for the user to select the type of movie they like. This is just an example program, as I want to understand throwing exceptions better. When the user clicks the display button it shows the type of movie they selected, between Action or Comedy. If no selection is made it throws an exception, this is the best way I could figure it out, is this going in the right direction?

 string selection;
        try
        {
            if (radAction.Checked)
            {
                selection = radAction.Text;
            }
            else
                if (radComedy.Checked)
                {
                    selection = radComedy.Text;
                }

                else
                    throw new ArgumentNullException("Please Choose Movie Type");
            MessageBox.Show(selection);
        }

        catch(ArgumentNullException msg)
        {
            MessageBox.Show(msg.Message);
        }
Charles Whitfield
  • 477
  • 1
  • 4
  • 7
  • Msgbox is not enough ? – matzone May 18 '13 at 16:12
  • This example is not good enough for understanding throwing exception. – Guanxi May 18 '13 at 16:15
  • @Guanxi - I know. I just wanted to know how to catch if a radio button is not been selected. – Charles Whitfield May 18 '13 at 16:16
  • put the complete case in Try block ... its already edited for that – Guanxi May 18 '13 at 16:19
  • 1
    And if you are going to raise exceptions for your own exceptional circumstances, define an exception for it, donlt throw a standard one. ArgumentNullException is for when you call foo(x) and x is never expected to be null, not user hasn't selected something in UI. – Tony Hopkinson May 18 '13 at 16:19
  • @TonyHopkinson - Thanks. How would I go about checking to see if a control hasn't been selected in UI, in your opinion how would you do it? – Charles Whitfield May 18 '13 at 16:25
  • Set first value of selection variable. – JustWork May 18 '13 at 16:27
  • Disable the next action(s) until one of them is. Check and throw a message in the next action if it isn't. This is not an exceptional circumstance. It's a poor user experience... – Tony Hopkinson May 18 '13 at 16:30
  • Or have a default, or a nothing selected option. I wouldn't use two radio buttons to indicate three states... – Tony Hopkinson May 18 '13 at 16:34
  • 1
    If the user isn't allowed to not make a selection, don't even give them the possibility of doing so. Use two radio buttons and write your UI so that one of them is always selected. Then the user can't fail to choose one. – Matthew Watson May 18 '13 at 17:06

2 Answers2

0

It's not at all a good practice to show Error message in your scenario. Using try...catch and throw...Exception always comes with a performance penalty. Try to avoid as much as possible. Refer this SO post for further reference.

But if you are really stick with try...catch then create your own User-defined exception.

 public class MovieSelectionNotFoundException : Exception
 {
     public MovieSelectionNotFoundException()
     {
     }

     public MovieSelectionNotFoundException(string message)
         : base(message)
     {
     }

     public MovieSelectionNotFoundException(string message, Exception inner)
         : base(message, inner)
     {
     }
 }

And you can use this in your code as follows:

string selection = string.Empty;
try
{
    if (radAction.Checked)
    {
        selection = radAction.Text;
    }
    else if (radComedy.Checked)
    {
        selection = radComedy.Text;
    }

    else
        throw new MovieSelectionNotFoundException("Please Choose Movie Type");

    MessageBox.Show(selection);
}

catch (MovieSelectionNotFoundException msg)
{
    MessageBox.Show(msg.Message);
}
Community
  • 1
  • 1
Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
0

Exceptions are for unexpected and exceptional situations. If you know the case, you should control that case and avoid using exceptions.

my requirements specifically call for try, catch

Having a try catch block is a good thing for the worst case scenarios, but you can still satisfy your requirements without throwing an exception. But do you really need to throw exception, when you can just control the case with a simple condition.

If you really need to throw an exception, you can implement a custom exception or use the ones already implemented under System.Exception.

Semih Yagcioglu
  • 4,011
  • 1
  • 26
  • 43