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

Erwin Bolwidt ejb@klomp.org
Sun May 27 11:59:00 GMT 2001


Hello list,

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

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

So how would you write a CNI method that does the same as this Java
method:

void method()
{
    Writer w = new FileWriter(FILE);
    try {
       w.write("something".toCharArray());
    }
    finally {
       w.close();
    }
}

I can only think of:

void SomeClass::method()
{
   java::io::Writer * w = new java::io::FileWriter(SomeClass::FILE);
   try {
       w->write(SomeClass::SOMETHING->toCharArray());
       w->close();
   }
   catch (java::lang::Throwable *  e) {
       w->close();
   }
}

... but you would have to copy the finally block to every possible
location where the try block could exit (with a break, return, etc.)

Could someone advise a nicer way to do this? It would help to automate
translation from Java to CNI methods in C++ where this would be useful for
speed or functionality purposes.


Best,
  Erwin Bolwidt



More information about the Java mailing list