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

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



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.


Again, constructor is used to allocate resources.
It is nonsense to use finally to unconditionally release the resourses
you
need to allocate.

Hence "along with a success flag".

You do not want to use finally, therefore.

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 can't remember the last time I needed a finalizer - they're very
rare.

If GC does all the deallocation for you means you do not allocate
anything
besides memory. You have no need to create a destructor.

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.


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). 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.


.



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: Go ahead. Stop programming. This ensures you from any mistakes.
    ... rolled back when a constructor throws an exception. ... IDisposable is *not* a destructor. ... Macros are code generators. ... static void PrintMe ...
    (microsoft.public.dotnet.languages.csharp)
  • 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: Bypassing startup macros
    ... Is there a command line I can use to open it up without using the macros? ... "Paul B" wrote: ... >> Excel is now throwing an illegal exception preventing me from opening the ... >> Sion Smith ...
    (microsoft.public.excel.programming)