Yes, those methods are pretty much (*) the same. The only difference is that it's easy to put a breakpoint in the first one. I'd always go with the second unless I really needed to break there and only there (as opposed to immediately any exceptions of that type were thrown, which would be easy).
Even if I ever used the first, I'd put it back to the second form before committing the code.
Yes, those methods are pretty much (*) the same. The only difference is that it's easy to put a breakpoint in the first one. I'd always go with the second unless I really needed to break there and only there (as opposed to immediately any exceptions of that type were thrown, which would be easy).
Even if I ever used the first, I'd put it back to the second form before committing the code. (*) There may well be some differences in terms of how the JIT handles them. The first will end up with more IL, which will affect opportunities for inlining etc.EDIT: I can't resist a bit of micro-benchmarking.
It looks like try/catch/throw has nastier effects on performance than just disabling inlining: using System; using System. Diagnostics; using System.Runtime. CompilerServices; public class Test { const int Iterations = 1000000000; static void Main() { Stopwatch sw; sw = Stopwatch.StartNew(); for (int i=0; I WriteLine("No inlining: {0}", sw.
ElapsedMilliseconds); sw = Stopwatch.StartNew(); for (int i=0; I ElapsedMilliseconds); } static void SimpleMethod() { Foo(); } MethodImpl(MethodImplOptions. NoInlining) static void NoInlining() { } static void TryCatchThrow() { try { Foo(); } catch (Exception) { throw; } } static void Foo() {} } Compile with /o+ /debug- Results (three runs): Simple method: 504, 495, 489 No inlining: 2977, 3060, 3019 try/catch/throw: 5274, 4543, 5145.
I believe any try/catch will prevent inlining. Researching ... – JaredPar Jan 10 '09 at 21:24 That was my guess too, though I didn't have the energy for research. Not that there's anything to stop the JIT from noticing that the try/catch is redundant in this case, I suspect, and stripping it... – Jon Skeet Jan 10 '09 at 21:26 Can't find anything to back up my assertion.
I believe it was either throwing or try/catch prevents inline. I suspect it may actually be throw vs. catch. – JaredPar Jan 10 '09 at 21:30 nastier effects?(5274-504)/1000000000 = no effect when you start doing something in foo() – iik Jan 10 '09 at 23:29 @iik: When you start doing anything particularly significant in Foo(), inlining will go away anyway.
However, in some cases it can be significant. If it weren't worth doing, MS wouldn't make the JIT capable of inlining, would they? – Jon Skeet Jan 10 '09 at 23:43.
No, you do not need the try catch. The only reason you would want to use the first function is if you wanted to do some logging or release resources before handling the function further up.
These two methods are essentially the same. In this case ReSharper was correct with its suggestion.
They really are not the same. Throw on its own or throw ex mess with the stack trace information and can make debugging harder. The best reason to catch an exception is to add context to the stack trace, like: try { Foo(); } catch (Exception e) { throw new BetterException("I did something slightly silly", e); }.
ReSharper is correct. Unless you actually intend to catch and do something about an exception there is no point including a try..catch block.
Unless you need the finally block to do some cleanup work. – Davin Studer Mar 4 '10 at 19:35 1 @Fred: That would be try..finally then not a try..catch, right? – AnthonyWJones Mar 4 '10 at 22:53.
Yes, the try catch block is redundant. The stack will just be unwound until a try/catch is found to handle the exception.
If all you do is rethrow the exception you don't need the try/catch block. Generally you should only catch exceptions when you can handle them. Otherwise let them propagate upwards.
Something I thought about, but I wasn't sure 100% so I went to check. I was right, sometimes. Apparently, if you re-throw the exception, which is what your code is doing, you could end up changing the stack-trace.
First of all, if you were to write throw ex; that would reset the stack-trace. Second, even when writing throw; there can also be cases where information is missing. See this article and some user comments following up on it.
Of course, most of these issues are related to the stack-trace and line-numbers, which are important, but I thought it would also affect performance, not just because of inlining (or the lack thereof), but also because of the whole exception catching and throwing overhead, but I didn't find anything concrete on this.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.