4 ms·
The TRY-CATCH will still be required. USING will only dispose the object.
by nreece 15y ago
The TRY-CATCH will still be required. USING will only dispose the object.
- politician 15y agoI encourage you to download a copy of Reflector or dotPeek; a using statement is compiled into a try-finally with a null-checked call to Dispose.
- nreece 15y agoI meant, it doesn't really CATCH the error. You can't handle the error, just avoid it being thrown.
- skrebbel 15y agoIt's not an error, it's an exception. Also, the whole point of the original comment was to not catch the exception, but to have it pass down the stack, while providing safe cleanup anyway.
- nreece 15y agoException it is. And how does it pass down the stack? There won't be any trace of it without it being caught.
- bullsbarry 15y agoIf an exception isn't caught, the caller will receive an opportunity to catch it, and so on until it hits the top of the call stack and the default handling occurs (a nasty, unhelpful error message is displayed)
- bunderbunder 15y agoThe stack trace is generated at the moment the exception is created. Unnecessarily catching exceptions you don't intend to deal with is a habit that is much more dangerous to stack traces, because it's easy to miss the huge semantic distinction between // leaves the Exception's stack trace intact. catch (Exception ex) { throw; } and // Replaces the Exception's stack trace // with a trace to the current execution point. catch (Exception ex) { throw ex; }