0

I know how to use try catch finally blocks. Everybody does.

But I want to know the internal implementation of this great functionality. Does it work like a commit or rollback method in SQL?

Does using try catch lead to performance issue?

Do we need to use try catch for every single small function?

Does the .net program go one step ahead and keep the result in ram?

Could somebody please explain to us how .net framework handles this great useful feature? I want a deep technical answer. ( behind the scenes )

Prageeth Liyanage
  • 1,612
  • 2
  • 19
  • 41
  • There is no commit or rollback functionality. You have to implement that. As far as the implementation goes, it's complicated, but generally incurs no overhead if no exceptions are thrown. When they are thrown, there's an algorithm to look for catch blocks in the current function or higher up the chain (actually, in two phases, one up the stack and one back down the stack). This is expensive, but that's okay because it's for exceptional (i.e., hopefully rare) behavior. – siride Feb 12 '16 at 05:07
  • Don't use try/catch unless you are actually prepared to handle the error. If not, then let it bubble up to the caller. Most of the time, you will want it to bubble up. Using try/finally is good, though, so you can rollback anything or make sure certain activities happen regardless of exceptions. – siride Feb 12 '16 at 05:08
  • You can easily get the behind-the-scenes info, the source [is readily available](https://github.com/dotnet/coreclr/blob/master/src/vm/excep.cpp). Quite a mountain of code, it is the 3rd largest source file in the CLR. It ultimately piggy-backs onto SEH (Structured Exception Handling) on Windows, probably the kind of topic you really want to study. – Hans Passant Feb 12 '16 at 10:24

1 Answers1

0

Does it work like a commit or rollback method in SQL?

No, it doesn't. Clr doesn't save old values of variables. If you throw an exception clr just jumps back to the previous method in stack.

Does using try catch lead to performance issue?

Yes, it does. If you throw an exception, clr creates stacktrace. This is a very expensive operation. But just try/catch blocks don't have serious performance side effects.

Do we need to use try catch for every single small function?

It depends on your coding style.

Could somebody please explain to us how .net framework handles this great useful feature? I want a deep technical answer. ( behind the scenes )

There is a good article about it: http://codinglight.blogspot.ru/2008/08/recently-i-received-question-on.html

Alex
  • 172
  • 1
  • 6