Throw is a keyword in C + + that is used to throw exceptions. If you do not use the throw keyword, the try will not catch anything; the at () function mentioned in the previous section also uses the Throw keyword internally to throw an exception.
Throw can be used either in a standard library or in a custom function to throw the exception we expect. The Throw keyword syntax is:
throw exceptiondata;
Exceptiondata is the meaning of "exception data", which can be either an ordinary variable or an object, as long as it can be matched in a catch.
The following example shows how to use the Throw keyword:
#include <iostream>#include<string>using namespacestd;CharGet_char (Const string&,int);intMain () {stringstr ="C plus plus"; Try{cout<<get_char (str,2) <<Endl; cout<<get_char (str, -) <<Endl; }Catch(inte) { if(e==1) {cout<<"Index underflow!"<<Endl; }Else if(e==2) {cout<<"Index overflow!"<<Endl; } } return 0;}CharGet_char (Const string&STR,intindex) { intLen =str.length (); if(Index <0) Throw 1; if(Index >=len)Throw 2; returnStr[index];}
In the Get_char () function, we used the throw keyword, and if the subscript is out of bounds, an exception of type int is thrown: if it is underflow, the value of the exception data is 1, and if it is overflow, the value of the exception data is 2. In a catch, an exception of type int is captured and a different prompt is output based on the exception data.
Non-recommended usage
The Throw keyword, in addition to being used to throw an exception in the function body, can also be used between the function head and the function body to indicate the type of exception the function can throw. Some documents are referred to as exception lists. For example:
Double func (char param) throw (int);
This statement declares a function named Func, whose return value type is double, has a parameter of type char, and can only throw an exception of type int. If other types of exceptions are thrown, the try will not be caught and only the program can be terminated.
If you want to be able to throw multiple types of exceptions, you can separate them with commas:
Double func (char param) throw (int, char, exception);
If you do not want to limit the exception type, you can omit:
Double func (char param) throw ();
Thus, the Func () function can throw any type of exception that can be captured by a try.
Change the code in the previous example:
#include <iostream> #include <string>using namespace std;char Get_char (const string & , int) throw (char , exception); int Main () {string str = "C plus plus" ; Try {Cout<<get_char (str, 2) << Endl; Cout<<get_char (str, +) << Endl;} catch (int e) {if (e==1 ) {cout<< "Index underflow!" << Endl;} else if (e==2 ) {cout<< "Index overflow!" << Endl;}} return 0 ;} Char Get_char (const string &str, int index) throw (char , exception) {int len = str.length (); if (Index < 0 ) Throw 1 , if (index >= len) throw 2 ; return str[index];}
Running code in the IDE using GCC crashes when you execute to the 12th line. Although the Func function detects that the subscript is out of bounds, it knows that an exception has occurred, but because the throw restricts the function to throw only char, exception type exceptions, try will not catch the exception and can only be handed over to the system to process and terminate the program.
It should be explained that the C + + standard has not recommended the use of the throw keyword, because each compiler support for the throw is different, some directly ignore, do not accept the limit of throw, some will throw as a function signature, resulting in the reference function may be problematic. The above code crashes when running under GCC and ignores the Throw keyword's restriction on the exception type directly when running under VS, the try can catch the exception thrown by Get_char (), and the program does not crash.
C + + Learning 40 throws its own exception