Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i do Exception Handling in "Using" block?

Tags:

c#

mysql

public virtual IQueryable<Hubs> getribbons(bool includeChildObjects)
        {
            using (var dbHelper = new DbHelper())
            {
                DbDataReaderExtended reader = null;
                try
                {
                    const string sqlQuery = "Select * From [ribbons]";
                    reader = dbHelper.ExecuteReader(sqlQuery, CommandType.Text, true);
                    IList<Hubs> models = new List<Hubs>();
                    while (reader.Read())
                    {
                        var model = GetHubDataFromReader(reader);
                        if (includeChildObjects)
                        {
                            model.Satellites = GetAllSatellites(true,model.HubID).ToList();
                        }
                        models.Add(model);
                    }
                    return models.AsQueryable();
                }
                finally
                {
                    if (reader != null) { reader.Close(); }
                }
            }
        }

Here this functon is in Business logic Layer. Here i need to handle exceptions in catch block and log it there and after throw it to the function it has called first(Presentation Layer). and then in finally i need to close all the things.

Please show me how to do error handling Here.

i am very new to C# please give me some clarifications on this this will be very helpful for me.

Thanks in advance

like image 484
user3008105 Avatar asked Oct 20 '22 17:10

user3008105


2 Answers

Finally blocks will be called whether or not there is a throw.

Using statements will call IDisposable.Dispose() on anything that isn't null once the scope of the Using block is exited.

like image 150
RomSteady Avatar answered Oct 30 '22 02:10

RomSteady


By using a finally block, you can clean up any resources that are allocated in a try block, and you can run code even if an exception occurs in the try block. Typically, the statements of a finally block run when control leaves a try statement. The transfer of control can occur as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.

Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement. Alternatively, you can catch the exception that might be thrown in the try block of a try-finally statement higher up the call stack. That is, you can catch the exception in the method that calls the method that contains the try-finally statement, or in the method that calls that method, or in any method in the call stack. If the exception is not caught, execution of the finally block depends on whether the operating system chooses to trigger an exception unwind operation.

like image 36
Amit Avatar answered Oct 30 '22 03:10

Amit