C
cody
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?
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?