How would you write a finally clause in C++?

Tom Tromey tromey@redhat.com
Sun May 27 12:29:00 GMT 2001


>>>>> "Erwin" == Erwin Bolwidt <ejb@klomp.org> writes:

Erwin> I hope this is the right list to ask.  When you want to write a
Erwin> try {} finally {} clause in C++ in a CNI method, how would you
Erwin> do that?

It might not be the right list.  General C++ questions should be
referred elsewhere, probably one of the C++ newsgroups.

Erwin> I've searched through the source of gcc/java and gcc/expr.c,
Erwin> and I found support for TRY_FINALLY in the compiler support
Erwin> file gcc/expr.c; so it should be accessible for the C++
Erwin> compiler, right?

No, not necessarily.  Trees have to support the union of all the
languages supported by gcc.  That doesn't mean that all supported
languages can generate all supported tree types.

Erwin> So how would you write a CNI method that does the same as this
Erwin> Java method:
Erwin> void method()
Erwin> {
Erwin>     Writer w = new FileWriter(FILE);
Erwin>     try {
Erwin>        w.write("something".toCharArray());
Erwin>     }
Erwin>     finally {
Erwin>        w.close();
Erwin>     }
Erwin> }

One approach would be to introduce a new class and put the cleanup
code in the destructor.

Tom



More information about the Java mailing list