473,414 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

[Proposal] using statement enhancements

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?
Nov 16 '05 #1
6 1704
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:


The usual idiom is something like:

Transaction t = Transcations.Begin(...);
bool committed = false;
try {
while ( still_not_done() )
t.Operate(...);
committed = t.Commit();
} finally {
if ( !committed )
t.Rollback();
}

Which nicely lets exception go to the caller of something goes wrong,
and completes cleanly otherwise.

--
Helge
Nov 16 '05 #2
Helge Jensen <he**********@slog.dk> wrote:
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:


The usual idiom is something like:

Transaction t = Transcations.Begin(...);
bool committed = false;
try {
while ( still_not_done() )
t.Operate(...);
committed = t.Commit();
} finally {
if ( !committed )
t.Rollback();
}

Which nicely lets exception go to the caller of something goes wrong,
and completes cleanly otherwise.


Actually, the using statement covers this already:

using (Transaction t = Transactions.Begin(...))
{
.. do stuff ..
t.Commit();
}

Every transaction class I've seen implements IDisposable in a way such
that if it's disposed without committing, the transaction is rolled
back, and if it's disposed after committing that's a no-op.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
cody <de********@gmx.de> wrote:
Mostly always you use the using clause you deal with native ressources, so
exception handlinjg is sooner or later inevitable.
Well, I don't agree that exception handling is in any way inevitable
*in the same scope as the using statement*, but I agree it happens.
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)
{
}


Yup, that would be nice for the cases where you *do* need to handle
exceptions at the same level.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
For your first suggestion:
why don't we allow catch blocks directly attached to the using clause:

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


You have a problem when it comes to providing a useful error message in the
event of a syntax error. This is an important issue with the IDE as well.
For example, let's assume this syntax exists. Also, let's assume that
try-catch also continues to exist. The following errant code is entered in
the IDE. Where should the error be flagged? (At point one or point two?)

public void mycall()
{
try
{ // do some stuff
//point one
using (File f = File.CreateText("bla.txt"))
{
} // point two
catch (IOExcepion e)
{
}
} // point three

I didn't put in indentation on purpose... the compiler ignores indentation.

OK... so where's the bug? If an close brace is added at point one, then the
catch goes with the using. If the close brace is added at point two, then
the catch goes with the try. Assume there is a LOT of code in here. The
IDE could easily instruct the developer to add a brace at the wrong place.
In fact, it would be difficult to catch the error before point three (end of
the method)!

This is one reason why block groups (like "catch" and "else") are not reused
in multiple structures.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Nov 16 '05 #5
> You have a problem when it comes to providing a useful error message in
the event of a syntax error.


You must be joking if that is the only problem you have with that :)
In my experience, a missing } often lets the compiler point to the end of
the file or the beginning of the next class, even if the brace is missing
somewhere in the middle of the class. I have no problem with that because it
happens very rarely and usually its no problem to spot the error.
Nov 16 '05 #6
I'm really glad that you are not designing a language that I would have to
actually use, Cody.

I've been writing code professionally since the early 80's. The list of
languages is long, and the list of tools to write them in is longer.

I can tell you one thing: making a language less usable because you think
your structure is cool... will get you a dead language.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"cody" <de********@gmx.de> wrote in message
news:OO*************@TK2MSFTNGP10.phx.gbl...
You have a problem when it comes to providing a useful error message in
the event of a syntax error.


You must be joking if that is the only problem you have with that :)
In my experience, a missing } often lets the compiler point to the end of
the file or the beginning of the next class, even if the brace is missing
somewhere in the middle of the class. I have no problem with that because
it happens very rarely and usually its no problem to spot the error.

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Paul Rubin | last post by:
I frequently find myself writing stuff like # compute frob function, x has to be nonnegative x = read_input_data() assert x >= 0, x # mis-use of "assert" statement frob = sqrt(x)...
31
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
8
by: Micah Elliott | last post by:
I also have this living as a wiki <http://tracos.org/codetag/wiki/Pep> if people would like to add comments there. I might try to capture there feedback from this group anyway. First try at a PEP...
4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from...
10
by: cody | last post by:
What if i want to validate the return value of a method but this method has serveral points where a return statement is. I know in MSIL there is only one RET opcode in each method and this means...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.