#1
|
|||
|
Exception Handling
Are C# try-catch blocks very expensive? Seems to me the only cost is really the code inside it, plus some constant for flagging it a "try." Doesn't seem much different than using conditional "if" statements except it is a bigger blanket and non-specific . I guess that is a pretty big difference depending on what you are doing ^^ anyhoo, just curious!
__________________
<Millenial Snowfkake Utopia>
| ||
|
#2
|
||||
|
Not a C person myself, but I thought try/catch statements were less performant than if/else statements, so I looked it up ...
https://stackoverflow.com/questions/...eption-handler From the second answer: Quote:
__________________
Loramin Frostseer, Oracle of the Tribunal <Anonymous> and Fan of the "Where To Go For XP/For Treasure?" Guides Anyone can improve the wiki! If you are new to the Blue server, you can improve the wiki to earn a "welcome package" of up to 2k+ platinum! Message me for details. | |||
|
#3
|
|||
|
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"); } } 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>
| ||
|
#4
|
|||
|
forgot to mention, the constant time assumes there is no exception thrown. if there is an exception thrown then the cost would be linear time as a function of th number of instructions in the block, no? Basically if an exception is thrown near the end of a try catch block, then you wasted a bunch of time that could have been saved by one or more conditionals (unless it can't be results over that way). I'd think it'd be more common for an exception to be thrown at the beginning of a try block though since it isn't really used unless you expect a potential exception throw in the next line of code. And, if you expect exceptions later on, you'd have to introduce conditionals to resolve those if you didn't use a try-catch block, no?
__________________
<Millenial Snowfkake Utopia>
| ||
|
#5
|
|||||
|
Well again, not a C guy, so I can't comment on the performance specifics of try/catch vs. if/else in C. I'm almost certain that there is some sort of cost to try/catch, but I'm also a very strong believer in one of Donald Knuth's famous quotes:
Quote:
(As an aside, a very common misconception among programming learners is that your computer/compiler is the primary "reader" of your source code. This misconception isn't actually one at first, when you're just working on your own code and learning. However, in a professional environment your code gets read by one or more peer reviewers when you first check it in, then again by you or someone else in the future everytime a bug gets fixed or a new feature gets added that relates to that code. If you piss off the compiler with uncompilable code you can fix it in a few seconds, but if you piss off the humans with unreadable you can waste hours if not days of people's time.) As for: Quote:
__________________
Loramin Frostseer, Oracle of the Tribunal <Anonymous> and Fan of the "Where To Go For XP/For Treasure?" Guides Anyone can improve the wiki! If you are new to the Blue server, you can improve the wiki to earn a "welcome package" of up to 2k+ platinum! Message me for details. | ||||
Last edited by loramin; 08-07-2018 at 11:35 AM..
|
|
#6
|
|||
|
that makes sense ^^ I thought about refactoring that whole method because I'm sure there's got to be a more direct way to do what im doing there. I'd considered peeling the one repeated item into its own method, but left it since it was one line of code repeated once. Of course if I introduce additional parameters or dump the try-catch block, I think I will roll into all into its own method. Im gonna remember that acronym ^^ I liek it ^^
__________________
<Millenial Snowfkake Utopia>
| ||
|
#7
|
||||
|
Quote:
__________________
Loramin Frostseer, Oracle of the Tribunal <Anonymous> and Fan of the "Where To Go For XP/For Treasure?" Guides Anyone can improve the wiki! If you are new to the Blue server, you can improve the wiki to earn a "welcome package" of up to 2k+ platinum! Message me for details. | |||
|
#8
|
||||
|
Quote:
__________________
<Millenial Snowfkake Utopia>
| |||
|
|
|