View Single Post
  #3  
Old 08-07-2018, 09:38 AM
maskedmelon maskedmelon is offline
Planar Protector

maskedmelon's Avatar

Join Date: Nov 2011
Location: not far from here
Posts: 5,795
Smile

thanks lor! I've seen that claim and also that it doesn't matter and was wondering more about the complexity of a try-catch block so that I can weigh its cost against alternatives ^^ liek, in some situations, i may need to write more instructions to code around it. the specific situation that I was dealing with:

Code:
        public static void Execute(string command)
        {
            UserCommand Commands = new UserCommand();
           
            try
            {
                if (command.Contains(" "))
                {
                    string filePath = command.Substring(command.IndexOf(" ") + 1);
                    command = command.Substring(0, command.IndexOf(" "));
                    int commandIndex = UserCommand.CommandList.IndexOf((from x in UserCommand.CommandList where x.Equals(command, StringComparison.OrdinalIgnoreCase) select x).First());
                    //if (UserCommand.CommandList.Any(x => x.Equals(command, StringComparison.OrdinalIgnoreCase)))         
                    Commands.GetType().GetMethod(UserCommand.CommandList[commandIndex], new Type[] { typeof(string) }).Invoke(Commands, new object[] { filePath });
                }
                else
                {
                    int commandIndex = UserCommand.CommandList.IndexOf((from x in UserCommand.CommandList where x.Equals(command, StringComparison.OrdinalIgnoreCase) select x).First());
                    Commands.GetType().GetMethod(UserCommand.CommandList[commandIndex], new Type[0]).Invoke(Commands, new object[0]);
                }
            }
            catch
            {
                Console.WriteLine("Invalid input");
            }
        }
if I didn't utilize the try-catch block, then I'd have to include two copies of that "if" line that i have commented out in addition to their else clauses and possible more conditionals if i added parameters to the method.

The try-catch block at least appeared to me to run in constant time, but I wasn't sure if there was some other mystical voodo beneath the hood that increased the complexity of it.
__________________
<Millenial Snowfkake Utopia>