[C con Clase] try...catch...finally

lsri8088 lsri8088 en gmail.com
Vie Mayo 18 17:16:53 CEST 2007


En el VB .NET (y creo que tb en el Visual C++) el bloque try/catch
tiene un bloque finally que se ejcuta siempre, se produzca o no la
excepcion.

¿y en C++ builder? No encuentro nada, parece que no existe. Esta el
__finally pero si lo pones despues de un catch no funciona (da error
de compilacion). En la ayuda meten un try dentro de otro. No lo
entiendo. ¿se hace asi?

Esto es de la ayuda:
...
try
  {
    try
    {
      f = 1.0;
      g = 0.0;
      try
      {
        puts("Another exception:");

        e = f / g;
      }
      __except(EXCEPTION_EXECUTE_HANDLER)
      {
        puts("Caught a C-based exception.");
        throw(Exception("Hardware error: Divide by 0"));
      }
    }
    catch(const Exception& e)
    {
      printf("Caught C++ Exception: %s :\n", e.msg());
    }
  }
  __finally
  {
    puts("C++ allows __finally too!");
  }
....


Running the above program results in the following:

Another exception:
Caught a C-based exception.
Caught C++ exception[Hardware error: Divide by 0]
C++ allows __finally too!




Más información sobre la lista de distribución Cconclase