Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you catch exceptions with "using" in C#

Tags:

c#

sql

ado.net

Given this code:

using (var conn = new SqlConnection("...")) {     conn.Open();     using (var cmd = conn.CreateCommand())     {         cmd.CommandText = "...";         using (var reader = cmd.ExecuteReader())         {             while (reader.Read())             {                 // ...             }         }     } } 

I'm used to writing try/catch/finally blocks for my data access, however, I'm being exposed to "using" which seems like a much simpler method of doing this. However, I'm trying to figure out how to catch exceptions that may occur.

Could you please give me an example of how you'd catch exceptions?

Edited to add:

I'm being led to believe that "using" is a replacement for my try/catch/finally blocks. I understand that using doesn't catch exceptions. So how is this a replacement?

like image 371
GregD Avatar asked Feb 05 '09 22:02

GregD


People also ask

How to perform exception handling with multiple catch in C++?

To perform exception handling with multiple catch. Step 1: Start the program. Step 2: Declare and define the function test (). Step 3: Within the try block check whether the value is greater than zero or not.

What are exceptions in C++?

Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution. Asynchronous Exception (Ex: which are beyond the program’s control, Disc failure, etc). try: represents a block of code that can throw an exception. catch: represents a block of code that is executed when a particular exception is thrown.

Is it possible to catch exceptions inside a using block?

The using statement is a shorthand for a try/finally block, but provides no mechanism for handling exceptions. This code is equivalent to a try/catch/finally, but does the "right thing" in the finally block automagically. @marc_s Yes, it is obvious how to catch exceptions inside using block.

How do you handle exceptions in your code?

Let the exception happen at this level, and allow it to bubble up the stack to code that's in a better position to know how to respond to it. In other words, leave your code sample as it is.


1 Answers

using isn't designed to catch exceptions; it's designed to give you an easy way to wrap a try/finally around an object that needs to be disposed. If you need to catch and handle exceptions then you'll need to expand it into a full try/catch/finally or put a containing try/catch around the whole thing.


To answer your edit (is using a replacement for try/catch/finally?) then no, it isn't. Most of the time when using a disposable resource you aren't going to handle the exception there and then because there's normally nothing useful you can do. So it provides a convenient way to just ensure that the resource is cleaned up irrespective of what you're trying to do works or not.

Typically code that deals with disposable resources is working at too low a level to decide what the correct action is on failure, so the exception is left to propagate to the caller who can decide what action to take (e.g. retry, fail, log, etc.). The only place where you'd tend to use a catch block with a disposable resource is if you're going to translate the exception (which is, I assume, what your data access layer is doing).

like image 194
Greg Beech Avatar answered Sep 22 '22 12:09

Greg Beech