try-catch statement
Aus cppreference.com
[] |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
Verwendet werden, um die Ausfhrung einer Verbindung-Anweisung versucht, whrend Fang und Behandlung von Ausnahmen, die als Ergebnis dieses Versuchs geworfen haben kann .
Original:
Used to attempt the execution of a compound-statement, while catching and handling exceptions that may have been thrown as a result of this attempt.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Syntax
try { statements } catch ( exception-decl ) { statements }
|
(1) | ||||||||
try { statements } catch ( exception-decl-1 ) { statements } catch ( exception-decl-2 ) { statements }
|
(2) | ||||||||
try { statements } catch ( exception-decl ) { statements throw; }
|
(3) | ||||||||
try { statements } catch ( ... ) { statements }
|
(4) | ||||||||
try : ctor-init-list { statements } catch ( exception-decl ) { statements }
|
(5) | ||||||||
Erklrung
- SieheAusnahmen auslsenfr weitere Informationen ber Ausnahmen auslsenOriginal:throw exceptionsThe text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.Original:SeeAusnahmen auslsenfor more information about throw exceptionsOriginal:throw exceptionsThe text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Die grundlegende Syntax (1) zeigt die grundlegenden Komponenten eines try-catch-Block. Zunchst leitet das Schlsselwort
try einen try-Block, in dem statements ausgefhrt werden knnen. Beachten Sie, dass der try-Block, von der ffnung geschweiften Klammern in seine Schliestellung, die gleichen Scoping-Regeln wie jeder andere Bereich (zB Variablen innerhalb des try-Blocks deklariert sind nicht auerhalb von ihr zur Verfgung, darunter aus dem catch-Block, der folgt folgt ). Dann wirkt die catch Anweisung als Planer von der Art der Ausnahme gefangen, wenn innerhalb des try-Block geworfen werden. Die exception-decl hat die gleiche Syntax wie die Parameter-Deklaration innerhalb eines Single-Parameter-Funktion Erklrung (auer dass der Typ nicht durch void, ein unvollstndiger Typ oder ein rvalue-reference (seit C++11)). Schlielich wird die zusammengesetzte Anweisung { statements } welche folgen Sie den catch-Anweisung aufgerufen wird die Ausnahme-Handler und enthlt Aussagen in Reaktion auf die Ausnahme, die gefangen wurde ausgefhrt werden. Typische Exception-Handling-Code enthlt Protokollierung der Fehler, den Einsatz einer alternativen Methode zu dem, was im try-Block, oder Umverpackung die Ausnahme in einer anderen geworfenen Ausnahme mit zustzlichen Informationen versucht .Original:
The basic syntax (1) shows the basic components of a try-catch block. First, the keyword
try initiates a try-block in which statements can be executed. Note that the try-block, from the opening curly-brace to its closing, follows the same scoping rules as any other scope (e.g. variables declared within the try-block are not available outside of it, including from the catch-block that follows). Then, the catch statement acts as a specifier of the type of exception to be caught if thrown from within the try-block. The exception-decl has the same syntax as the parameter declaration within a single-parameter function declaration (except that the type cannot by void, an incomplete type, or an rvalue-reference (seit C++11)). Finally, the compound-statement { statements } which follow the catch-statement is called the exception-handler and contains statements to be executed in response to the exception that was caught. Typical exception-handling code includes logging the error, employing an alternative method to what was attempted in the try-block, or re-packaging the exception into another thrown exception with additional information.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
In Syntax (2), ist die Darstellung hier, dass ein try-catch-Block nicht auf einem einzigen catch-Block begrenzt. Da verschiedene Arten von Ausnahmen innerhalb des try-Blocks ausgelst werden kann, kann ein Geben Sie so viele Catch-Blcke wie ntig, um alle Ausnahmen wnscht man sich zu handhaben handhaben. Beachten Sie jedoch, dass die Reihenfolge des Auftretens der catch-Statements wichtig ist, wird eine ausgelste Ausnahme von der ersten catch-Block behandelt werden, indem der Reihenfolge ihres Auftretens, deren exception-decl ist ein gltiges Spiel (und implizite Konvertierungen gelten auch). In anderen Worten, es ist nicht die besten Match, gewhlt (wie in berladen von Funktionen Regeln), aber der ersten Match. Wenn die ausgelste Ausnahme entspricht keiner der catch-Statements, dann wird die Ausnahme zurck, bis ein anderer umschlieenden try-Block erreicht ist oder bis das Programm beendet wird aufgrund einer nicht behandelten Ausnahme durchgefhrt .
Original:
In syntax (2), the illustration here is that a try-catch block is not limited to a single catch-block. Since different types of exceptions can be thrown from within the try-block, one can specify as many catch-blocks as necessary to handle all exceptions one wishes to handle. Note, however, that the order of appearance of the catch-statements is important, a thrown exception will be handled by the first catch-block, by order of appearance, whose exception-decl is a valid match (and implicit conversions apply as well). In other words, it is not the best match that is chosen (as in function overloading rules), but the first match. If the thrown exception matches none of the catch-statements, then the exception is carried back until another enclosing try-block is reached or until the program is terminated due to an unhandled exception.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
In Syntax (3), ist die einzige Ergnzung der
throw; Anweisung innerhalb der catch-Block. Die Erklrung hat den Effekt wieder werfen die gleiche Exception-Objekt, das von der catch-Block abgefangen wurde. Dies ist der einzige Zusammenhang, in dem ein leerer Einwurf Erklrung gilt, und es ist besondere Bedeutung wieder werfen die Ausnahme, die gefangen wurde und erklrte, wie exception-decl .Original:
In syntax (3), the only addition is the
throw; statement within the catch-block. The statement has the effect of re-throwing the same exception object which was caught by the catch-block. This is the only context in which an empty throw-statement is valid, and it's specific meaning is to re-throw the exception that was caught, and declared as exception-decl.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Syntax (4) ist eine sogenannte catch-all-Block. Das Ellipse Betreiber
... (wrtlich drei Punkte) knnen in-place des exception-decl angeben, dass smtliche Arten von Ausnahmen von der catch-Block sollte gefangen werden verwendet werden. Diese Art der catch-all-Block ist nicht immer keine Informationen ber die Art der Ausnahme, die ausgelst wurde ntzlich, und da die meisten Ausnahme-Objekte von Klassen aus std :: Ausnahme
abgeleitet sind, ist es im Allgemeinen sinnvoll, die Ausnahme, da Original:
std::exception
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
catch( std::exception& e ) fangen. Allerdings ist der Hauptzweck einer catch-all-Block, um sicherzustellen, dass keine abgefangene Ausnahmen von einer Funktion, was besonders ntzlich ist fr spezielle Funktionen, aus denen undichte Ausnahmen gefhrlich sein, vor allem kann ein Destruktor oder ein dynamisch verlinkten sind durchgesickert Funktion .Original:
Syntax (4) is a so-called catch-all block. The ellipsis operator
... (literally, three dots) can be used in-place of the exception-decl to specify that any and all types of exceptions should be caught by the catch-block. This type of catch-all block is not useful for getting any information on the type of exception that was thrown, and since most exception objects are of classes derived from std :: Ausnahme
, it is generally more useful to catch the exception as Original:
std::exception
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
catch( std::exception& e ). However, the main purpose of a catch-all block is to ensure that no uncaught exceptions are leaked from a function, which is especially useful for special functions from which leaking exceptions can be dangerous, most notably, a destructor or a dynamically-linked external function.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Syntax (5) wird als Funktions-try-block und kann verwendet werden, um eine ganze Funktion Krper in einem try-Block einzuschlieen (mit catch-Blcke folgen it). Dies ist besonders ntzlich, um Ausnahmen, die whrend der Ausfhrung eines Konstrukteurs-Initialisierung Liste (Bau von Sub-Objekte einer Klasse), in der Tat geworfen werden konnte fangen, ist es der einzige Weg, dies zu tun. Diese Funktions-try-Block Syntax wird selten in einem anderen Zusammenhang verwendet, weil es keinen Vorteil hat im Gegensatz zu einem traditionellen try-catch-Block gegenber, und die daraus resultierende Syntax ist in der Regel unattraktiv (und ungewohnt fr die meisten) .
Original:
Syntax (5) is called a function-try-block and can be used to enclose an entire function body inside a try-block (with catch-blocks following it). This is especially useful to catch exceptions which could be thrown during the execution of a constructor's initialization list (construction of sub-objects of a class), in fact, it is the only way to do so. This function-try-block syntax is seldom used in any other context because it has no advantage as compared to a traditional try-catch block, and the resulting syntax is generally unappealing (and unfamiliar to most).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Keywords
Beispiel
Das folgende Beispiel zeigt mehrere Anwendungsflle des
try-catch Block
Original:
The following example demonstrates several usage cases of the
try-catch block
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <iostream>
#include <vector>
int main()
{
try {
std::cout << "Throwing an integer exception...\n";
throw int(42);
} catch( int i ) {
std::cout << " the integer exception was caught, with value: " << i << '\n';
}
try {
std::cout << "Creating a vector of size 5... \n";
std::vector<int> v(5);
std::cout << "Accessing the 11th element of the vector...\n";
v.at(10); // the at() function will check the range.
} catch( std::exception& e) {
std::cout << " a standard exception was caught, with message '" << e.what() << "'\n";
}
}
Output:
Throwing an integer exception...
the integer exception was caught, with value: 42
Creating a vector of size 5...
Accessing the 11th element of the vector...
a standard exception was caught, with message 'out_of_range'