Re: Go ahead. Stop programming. This ensures you from any mistakes.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



valentin tihomirov <V_tihomirov@xxxxxxx> wrote:
Please explain how the above code prints "10" if object creation is
rolled back when a constructor throws an exception.

Because you do not roll back the assignment.

But if the object itself were "uncreated" then that wouldn't make any
odds.

The object is clearly still alive, contrary to your previous statement.

No, you don't want to use a finally and rollback *unconditionally*.
Just keep a single boolean flag to say whether or not the whole
operation succeeded, and make each finally block check it. Simple.

Peahaps, the try-catch is in your list of discouraged constructrions along
with goto. That is why you prefer finally with a flag instead of the
construct, which intentionally intorduced to take action in case of error.

I'm perfectly happy to use try/catch where appropriate. Using try/catch
and then rethrowing is another alternative to using try/finally.

So, now we have two different ways, neither of which require using
fallthrough/"goto" in a switch/case statement. So much for it being
indispensible.

I use plenty of non-memory resources, but don't rely on the GC to
release them - I use the IDisposable pattern.

Which is a destructor. I told nothing about a need to use finalizers.

No, IDisposable is *not* a destructor. I believe that there's some
relationship in C++/CLI between IDisposable and destructors, but in C#
(post v1) there is no such thing as a destructor. In particular, there
is nothing which will automatically be called in C# at the end of the
scope of a variable.

The use of macros has made life incredibly painful for millions of
developers, which is why macros don't tend to be in modern languages.

Improperly, unconsciously applied hammer can be even more painful and
dangerous. Macros are code generators. Using them greatly reduces amount of
code (program redability).

You seem to take it for granted that reducing the amount of code
improves code readability. Quite often I'll find that the most readable
form of some code is *not* the shortest.

The alternative of not using them in C is loads
of code. You can live without them in OOP languages because we have
exceptions supported at language level. Nevertheless, occasionally, the lack
of macros is observed. For instance, in logging
public void log (int severity, str message) {
if (severity >= debug_level)
print(message);
}

you have a lot of log function invocations. If the debug level is high
(normally, you log only errors), the function will do nothing. According to
Log4j project, the most logging time is spent on constructing the text
message. So, you construct a message to discard it. And the function is
public, so it cannot be inlined. In Delphi, which lacks ternary operators, I
also felt the lack of macros to implemement it. However in C, avoiding
macros is huge waste of labour and storage resourses.

Perhaps you haven't seen the Conditional attribute.

using System;
using System.Diagnostics;

class Test
{
static void Main()
{
PrintMe(ConstructMessage());
}

static string ConstructMessage()
{
Console.WriteLine ("Constructing message");
return "This could have taken a long time to build.";
}

[Conditional("TESTING")]
static void PrintMe(string message)
{
Console.WriteLine(message);
}
}

Compile it with the "TESTING" symbol defined, and it'll print the two
strings. Compile it without the "TESTING" symbol defined, and nothing
will be printed.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: HLA and embedded controllers
    ... it has to test the return code in order to pass that exception back ... routines that wind up getting called in the middle of the call sequence ... same way with my design. ... which is stronger than MASM's recusive macros. ...
    (alt.lang.asm)
  • Re: The program that wouldnt Quit
    ... * Feeds the entire Mindprod website to the Replacer macro mechanism ... Expands HTML boilerplate refresher macros ... * @param dirName ... static void gather ...
    (comp.lang.java.programmer)
  • Re: GPIO support for HTC Dream
    ... This looks a bit dodgy. ... No, macros are macros, that means upcase. ... static int dream_suspended; ... static void __init dream_init ...
    (Linux-Kernel)
  • Re: exception handling in cobol /bs2000
    ... would be a way to go (before the ISO 2002 COBOL "exception handling" is ... >:>> for user written exection routines to handle any system interrupts. ... > You want macros? ...
    (comp.lang.cobol)
  • Re: Go ahead. Stop programming. This ensures you from any mistakes.
    ... rolled back when a constructor throws an exception. ... It is nonsense to use finally to unconditionally release the resourses ... which is why macros don't tend to be in modern languages. ... Macros are code generators. ...
    (microsoft.public.dotnet.languages.csharp)