c# - RadioButtons and Throwing Exception -


i have 2 radio buttons user select type of movie like. example program, want understand throwing exceptions better. when user clicks display button shows type of movie selected, between action or comedy. if no selection made throws exception, best way figure out, going in 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);         } 

it's not @ practice show error message in scenario. using try...catch , throw...exception comes performance penalty. try avoid as possible. refer so post further reference.

but if stick try...catch create 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 can use in code 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); } 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Socket.connect doesn't throw exception in Android -

SPSS keyboard combination alters encoding -