I\'ve heard the advice that you should avoid try catch blocks if possible since they\'re expensive.
My question is specifically about the .NET platform: Why are try
IMO this whole discussion is like saying "wow lops are expensive because I need to increment a counter... i'm not going to use them any more", or "wow creating an object takes time, i'm not going to create a ton of objects any more."
Bottom line is your adding code, presumably for a reason. If the lines of code didn't then incur some overhead, even if its 1 CPU cycle, then why would it exist? Nothing is free.
The wise thing to do, as with any line of code you add to your application, is to only put it there if you need it to do something. If catching an exception is something you need to do, then do it... just like if you need a string to store something, create a new string. By the same means, if you declare a variable that isn't ever used, you are wasting memory and CPU cycles to create it and it should be removed. same with a try/catch.
In other words, if there's code there to do something, then assume that doing something is going to consume CPU and/or memory in some way.
The compiler emits more IL when you wrap code inside a try/catch block; Look, for the following program :
using System;
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("abc");
}
}
The compiler will emit this IL :
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 13 (0xd)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "abc"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret
} // end of method Program::Main
While for the slightly modified version :
using System;
public class Program
{
static void Main(string[] args)
{
try { Console.WriteLine("abc"); }
catch { }
}
}
emits more :
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 23 (0x17)
.maxstack 1
IL_0000: nop
.try
{
IL_0001: nop
IL_0002: ldstr "abc"
IL_0007: call void [mscorlib]System.Console::WriteLine(string)
IL_000c: nop
IL_000d: nop
IL_000e: leave.s IL_0015
} // end .try
catch [mscorlib]System.Object
{
IL_0010: pop
IL_0011: nop
IL_0012: nop
IL_0013: leave.s IL_0015
} // end handler
IL_0015: nop
IL_0016: ret
} // end of method Program::Main
All these NOPs and others cost.
Every try needs to record a lot of information, e.g. stack pointers, values of CPU register, etc. so it can unwind the stack and bring back the state it has been when passing the try block in case an exception is thrown. Not only that every try needs to record a lot of information, when an exception is thrown, a lot of values needs to be restored. So a try is very expensive and a throw/catch is very expensive, too.
That doesn't mean you shouldn't use exceptions, however, in performance critical code you should maybe not use too many tries and also not throw exceptions too often.
A try block is not expensive at all. Little or no cost is incurred unless an exception is thrown. And if an exception has been thrown, that's an exceptional circumstance and you don't care about performance any more. Does it matter if your program takes 0.001 seconds or 1.0 seconds to fall over? No, it does not. What matters is how good the information reported back to you is so you can fix it and stop it happening again.
This is not something I would ever worry about. I would rather care about the clarity and safety of a try...finally block over concerning myself with how "expensive" it is.
I personally don't use a 286, nor does anyone using .NET or Java either. Move on. Worry about writing good code that will affect your users and other developers instead of the underlying framework that is working fine for 99.999999% of the people using it.
This is probably not very helpful, and I don't mean to be scathing but just highlighting perspective.
It's not try blocks you need to worry about as much as catch blocks. And then, it's not that you want to avoid writing the blocks: it's that you want as much as possible to write code that will never actually use them.