[Mono-list] Npgsql error

Emil Emilov emil at emilov.de
Fri Aug 5 04:03:29 EDT 2005


Ok, but how does an exception fit in? You need to know if there was an error to log it or do additional stuff (like MsgBox.Show etc.).

Jaroslaw Kowalski wrote:
>> Cool, a question on that:
>> What happens if a exception occurs?
>> I meand close() gets called at the end of the using block, but does it 
>> get also called when an exception occurs (sort of finally)?
>> If yes, I'm changing my programming style :)
>> Another question, how would it look like if you had to catch an 
>> exception (i.e. to rollback a transaction)?
> 
> 
> The following code:
> 
> ====
> using (A a = new A())
> {
>    code
> };
> ====
> 
> Is equivalent to:
> 
> =======
> A a = null;
> 
> try
> {
>    a = new A();
>    /// code
> }
> finally
> {
>    if (a != null) ((IDisposable)a).Dispose();
> }
> =======
> 
> It's the C# compiler that translates every using() to something like the 
> above code. The IDisposable.Dispose method on data providers 
> automatically rolls back any open and uncommitted transactions so you 
> don't need to do explicit rollbacks.
> 
> BTW. I've seen many (MS and other) examples where they don't call 
> Dispose the IDbCommand object, even though it implements IDisposable, 
> I'm not sure whether it leads to potential resource leaks or not.
> 

-- 
Emil R. Emilov
-----------------------------------------------------------------------
mailto:emil at emilov.de
http://www.emilov.de


More information about the Mono-list mailing list