__finally

Aus RAD Studio
Wechseln zu: Navigation, Suche

Nach oben zu Schlüsselwörter alphabetisch - Index


Kategorie

Anweisungen, Schlüsselwort-Erweiterungen

Syntax

__finally {compound-statement}

Beschreibung

Das Schlüsselwort __finally spezifiziert eine Aktion, die unabhängig davon durchgeführt werden soll, wie der Programmablauf durch das vorangehende __try-Konstrukt endet.


Beispiel

Das folgende Code-Fragment zeigt die Verwendung des try/__finally-Konstrukts:

#include <stdio.h>
#include <string.h>
#include <windows.h>

class Exception
{
public:
    Exception(char* s = "Unknown"){what = strdup(s);      }
    Exception(const Exception& e ){what = strdup(e.what); }
    ~Exception()                   {free(what);         }
    char* msg() const             {return what;           }
private:
    char* what;
};

int main()
{
    float e, f, g;
        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!");
        }
    return e;
}

Das obige Programm erzeugt die folgende Ausgabe:

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

Siehe auch