[Proposal] using statement enhancements

From: cody (deutronium_at_gmx.de)
Date: 03/05/05


Date: Sat, 5 Mar 2005 13:56:48 +0100

Mostly always you use the using clause you deal with native ressources, so
exception handlinjg is sooner or later inevitable.

so why do we need to write

try
{
  using (File f = File.CreateText("bla.txt"))
  {
  }
}catch (IOExcepion e)
{
}

why don't we allow catch blocks directly attached to the using clause:

using (File f = File.CreateText("bla.txt"))
{
}
catch (IOExcepion e)
{
}

It is much shorter and readable. One of the problems with Exceptions is that
is often produces very deep nested code and at the end you do not know which
belongs to what.

The second idea I have is that we need some support for transactions in
code. Currently we have to write huge statements like that to enable safe
transaction handling:

try
{
    BeginTransaction();
 // stuff
   Commit();
}
catch (SpecialException e)
{
   Rollback();
   throw new SpecialException ("Something fouled up here", e);
}
catch (Exception)
{
   Rollback();
   throw;
}

So I'd love to see that in future using would accept next to IDisposible a
new interface ITransaction:

interface ITransaction
{
  void BeginTransaction();
  void Commit();
  void Rollback();
}

so we could write code like that:

using (dbconnection.Transaction)
{
   // do stuff here
}

the property Transaction would return an (not necessarily a new) object
implementing ITransaction and BeginTransaction, Commit and Rollback are
automatically called. If the object additionally implements IDisposible
dispose is also called in the finally clause.

What do you think?



Relevant Pages

  • Re: Evaluating Exceptions, Try Except and Try Finally
    ... error occurs during execution. ... statementList2 (the finally clause) is executed. ... exception is raised during execution of statementList1, ... THE IF THEN ELSE STATEMENT DOCUMENTATION IS JUST AS FUCKED:)' ...
    (alt.comp.lang.borland-delphi)
  • Re: Finally in IE
    ... If an exception is thrown, each catch clause is inspected in turn ... the clause executes wether or not an exception is thrown ... Firefox considers your <script> block as self-enclosed system. ...
    (comp.lang.javascript)
  • Re: "ensure" hiding actual error
    ... You caused another exception inside your ensure clause, ... problems with handling correctly the original exception. ... IIRC initially this was about a syntax error. ...
    (comp.lang.ruby)
  • Re: Try Finally...
    ... > language than risk that happening. ... You're well versed in Windows exception handling, ... clause should handle a non-fatal exception. ... to non-fatal exceptions than they do now. ...
    (comp.lang.pascal.delphi.misc)
  • Transactions, eval, local and confusion
    ... I have read through the DBI docs and DBD::mysql docs a couple times and ... If the answer to number 1 is no, then presumably 'rollback' throws an ... setting AutoCommit to 0 before setting the RaiseError ... can put it after the RaiseError and let the eval catch its exception as ...
    (perl.dbi.users)