try-finally
Mergetry-finally
statement
try-finally
Instrukcja jest rozszerzeniem specyficznym dla firmy Microsoft , które obsługuje strukturalną obsługę wyjątków w językach C i C++.The try-finally
statement is a Microsoft-specific extension that supports structured exception handling in the C and C++ languages.
SkładniaSyntax
Poniższa składnia zawiera opis try-finally
instrukcji:The following syntax describes the try-finally
statement:
// . . .
__try {
// guarded code
}
__finally {
// termination code
}
// . . .
GramatykaGrammar
try-finally-statement
:try-finally-statement
:
__try
compound-statement
__finally
compound-statement
__try
compound-statement
__finally
compound-statement
try-finally
Instrukcja to rozszerzenie firmy Microsoft do języków C i C++, które umożliwiają aplikacjom docelowym wykonywanie kodu czyszczącego w przypadku przerwania wykonywania bloku kodu.The try-finally
statement is a Microsoft extension to the C and C++ languages that enable target applications to guarantee execution of cleanup code when execution of a block of code is interrupted. Oczyszczanie składa się z takich zadań, jak cofanie alokacji pamięci, zamykanie plików i zwalnianie dojść do plików.Cleanup consists of such tasks as deallocating memory, closing files, and releasing file handles. try-finally
Instrukcja jest szczególnie przydatna w przypadku procedur, które mają kilka miejsc, w których wykonywane jest sprawdzenie dla błędu, który może spowodować przedwcześnie powrót z procedury.The try-finally
statement is especially useful for routines that have several places where a check is made for an error that could cause premature return from the routine.
Aby uzyskać informacje pokrewne i przykład kodu, zobacz try-except
instrukcja.For related information and a code sample, see try-except
Statement. Aby uzyskać więcej informacji o ogólnym obsłudze wyjątków, zobacz strukturalna obsługa wyjątków.For more information on structured exception handling in general, see Structured Exception Handling. Aby uzyskać więcej informacji na temat obsługi wyjątków w aplikacjach zarządzanych przy użyciu języka C++/CLI, zobacz Obsługa wyjątków w /clr
temacie.For more information on handling exceptions in managed applications with C++/CLI, see Exception Handling under /clr
.
Uwaga
Strukturalna obsługa wyjątków działa z Win32 dla plików źródłowych C i C++.Structured exception handling works with Win32 for both C and C++ source files. Jednakże nie jest specjalnie zaprojektowana dla języka C++.However, it is not specifically designed for C++. Można zapewnić, że kod będzie bardziej przenośny przy użyciu obsługi wyjątków C++.You can ensure that your code is more portable by using C++ exception handling. Ponadto, obsługa wyjątków C++ jest bardziej elastyczna, gdyż może obsługiwać wyjątki dowolnego typu.Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. W przypadku programów C++ zaleca się użycie mechanizmu obsługi wyjątków C++ ( try
, catch
i throw
instrukcji).For C++ programs, it is recommended that you use the C++ exception-handling mechanism (try
, catch
, and throw
statements).
Instrukcja złożona po __try
klauzuli jest sekcją chronioną.The compound statement after the __try
clause is the guarded section. Złożona instrukcja po __finally
klauzuli to procedura obsługi zakończenia.The compound statement after the __finally
clause is the termination handler. Program obsługi określa zestaw akcji, które są wykonywane, gdy Sekcja chroniona zostanie zakończona, bez względu na to, czy opuszcza sekcję chronioną przez wyjątek (nietypowe zakończenie) lub według standardu (normalne zakończenie).The handler specifies a set of actions that execute when the guarded section is exited, whether it exits the guarded section by an exception (abnormal termination), or by standard fall through (normal termination).
Formant dociera do __try
instrukcji przez proste wykonywanie sekwencyjne (przechodzenie).Control reaches a __try
statement by simple sequential execution (fall through). Gdy kontrolka przejdzie do __try
, jego skojarzona procedura obsługi zostanie uaktywniona.When control enters the __try
, its associated handler becomes active. Jeśli przepływ sterowania osiągnie koniec bloku try, wykonanie przebiega w następujący sposób:If the flow of control reaches the end of the try block, execution proceeds as follows:
Procedura obsługi zakończenia jest wywoływana.The termination handler is invoked.
Po zakończeniu procedury obsługi zakończenia wykonywanie jest kontynuowane po
__finally
instrukcji.When the termination handler completes, execution continues after the__finally
statement. Jednak Sekcja chroniona kończy się (na przykład za pośrednictwemgoto
niechronionej treści lubreturn
instrukcji), program obsługi zakończenia jest wykonywany przed przejściem z sekcji chronionej przez przepływ sterowania.However the guarded section ends (for example, via agoto
out of the guarded body or areturn
statement), the termination handler is executed before the flow of control moves out of the guarded section.__finally
Instrukcja nie blokuje wyszukiwania odpowiedniego programu obsługi wyjątków.A__finally
statement doesn't block searching for an appropriate exception handler.
Jeśli w bloku wystąpi wyjątek __try
, system operacyjny musi znaleźć procedurę obsługi dla wyjątku lub program zakończy się niepowodzeniem.If an exception occurs in the __try
block, the operating system must find a handler for the exception or the program will fail. Jeśli zostanie znaleziona procedura obsługi, wszystkie __finally
bloki są wykonywane i wznawiają wykonywanie w programie obsługi.If a handler is found, any and all __finally
blocks are executed and execution resumes in the handler.
Załóżmy na przykład, że seria wywołań funkcji łączy funkcję A z funkcją D, jak pokazano na poniższej ilustracji.For example, suppose a series of function calls links function A to function D, as shown in the following figure. Każda funkcja ma jedną procedurę obsługi zakończenia.Each function has one termination handler. Jeśli wyjątek jest wywoływany w funkcji D i obsługiwany w, programy obsługi zakończenia są wywoływane w tej kolejności, w której system rozwinięcia stosu: D, C, B.If an exception is raised in function D and handled in A, the termination handlers are called in this order as the system unwinds the stack: D, C, B.
Kolejność zakończenia — wykonywanie programu obsługiOrder of Termination-Handler Execution
Uwaga
Zachowanie funkcji try-finally różni się od innych języków, które obsługują korzystanie z programu finally
, takich jak C#.The behavior of try-finally is different from some other languages that support the use of finally
, such as C#. Pojedynczy __try
może mieć jedną z nich, ale nie obie, z __finally
i __except
.A single __try
may have either, but not both, of __finally
and __except
. Jeśli oba te elementy mają być używane razem, zewnętrzna Instrukcja try-except musi zawierać wewnętrzną instrukcję try-finally.If both are to be used together, an outer try-except statement must enclose the inner try-finally statement. Reguły określające, kiedy każdy blok wykonuje się również inaczej.The rules specifying when each block executes are also different.
W celu zapewnienia zgodności z poprzednimi wersjami,, _try
_finally
i _leave
są synonimami dla __try
, __finally
i, __leave
Jeśli opcja kompilatora /Za
(Wyłącz rozszerzenia języka) jest określona.For compatibility with previous versions, _try
, _finally
, and _leave
are synonyms for __try
, __finally
, and __leave
unless compiler option /Za
(Disable language extensions) is specified.
Słowo kluczowe __leaveThe __leave Keyword
__leave
Słowo kluczowe jest prawidłowe tylko w sekcji chronionej try-finally
instrukcji i jego efektem jest przejście do końca sekcji chronionej.The __leave
keyword is valid only within the guarded section of a try-finally
statement, and its effect is to jump to the end of the guarded section. Wykonywanie jest kontynuowane na pierwszej instrukcji w programie obsługi zakończenia.Execution continues at the first statement in the termination handler.
goto
Instrukcja może również wyskoczyć z chronionej sekcji, ale obniża wydajność, ponieważ wywołuje odwracanie stosu.A goto
statement can also jump out of the guarded section, but it degrades performance because it invokes stack unwinding. __leave
Instrukcja jest bardziej wydajna, ponieważ nie powoduje odwinięcia stosu.The __leave
statement is more efficient because it doesn't cause stack unwinding.
Nietypowe zakończenieAbnormal Termination
Kończenie try-finally
instrukcji przy użyciu funkcji longjmp Run-Time jest uznawane za nietypowe zakończenie.Exiting a try-finally
statement using the longjmp run-time function is considered abnormal termination. Nie jest to dozwolone, aby przechodzić do __try
instrukcji, ale jest to dozwolone w przypadku przechodzenia z jednego.It isn't legal to jump into a __try
statement, but it's legal to jump out of one. Wszystkie __finally
instrukcje, które są aktywne między punktem wyjścia (normalne zakończenie __try
bloku) i miejsce docelowe ( __except
blok obsługujący wyjątek), muszą być uruchomione.All __finally
statements that are active between the point of departure (normal termination of the __try
block) and the destination (the __except
block that handles the exception) must be run. Jest on nazywany lokalnym odwinięciem.It's called a local unwind.
Jeśli __try
blok jest przedwcześnie zakończony z dowolnego powodu, włącznie z skokiem z bloku, system wykonuje skojarzony __finally
blok jako część procesu odwinięcia stosu.If a __try
block is prematurely terminated for any reason, including a jump out of the block, the system executes the associated __finally
block as a part of the process of unwinding the stack. W takich przypadkach AbnormalTermination
Funkcja zwraca w true
przypadku wywołania z __finally
bloku; w przeciwnym razie zwraca false
.In such cases, the AbnormalTermination
function returns true
if called from within the __finally
block; otherwise, it returns false
.
Procedura obsługi zakończenia nie jest wywoływana, jeśli proces zostanie zamknięty w trakcie wykonywania try-finally
instrukcji.The termination handler isn't called if a process is killed in the middle of executing a try-finally
statement.
ZAKOŃCZENIE specyficzne dla firmy MicrosoftEND Microsoft-specific
Zobacz teżSee also
Pisanie programu obsługi zakończeniaWriting a termination handler
Obsługa wyjątków strukturalnych (C/C++)Structured Exception Handling (C/C++)
Słowa kluczoweKeywords
Zakończenie — składnia programu obsługiTermination-handler syntax