, , , . , . . ++ .
++ , , <Crtl> +<C>. , .
.
, , , try:
try {
}
. , , . throw , . , :
throw [];
, . , try-, , .
, , . , throw .
catch, . try-. . :
Catch ( ) {/* */}
Catch () {/* */}
Catch () {/* */}
, - (, ). . , . .
catch (int i) {
}
catch (const *char) {
}
catch (Overflow) {
// Overflow
}
catch (j) {
//
}
, . , , , try- .
|
|
throw , ++ :
1) throw , , ;
2) , , ;
3) , , .
, , .
, , throw:
- , catch;
- , cacth.
, , . void .
# include <fstream.h>
class Hello { // ,
public:
Hello () {cout << Hello! << endl; }
~Hello () {cout << Bye! << endl; }
}
void f1 () {
ifstream ifs (\\INVALD\FILE\NAME); //
if (!ifs) {
cout << << endl;
throw ; }
}
void f2 () {
Hello H; //
f1 (); // ,
}
int main () {
try {
cout << try- << endl;
}
catch (int i) {
cout << int, << I << endl;
return -1;
}
catch (const char *p) {
cout << const char *, - << p << endl;
return -1;
}
catch (j) {
cout << << endl;
return -1;
}
return 0;
}
:
try-
Hello!
Bye!
const char *,
!!! , , f1 , main.
, .
throw, , :
void f1 () throw (int, const char*) {/* */}
void f2 () throw (Oops*) {/* */} // Oops* - Oops
throw , . , :
void f () throw () {
}
, . unexpected, terminate. terminate abort, .
|
|
, . . , , , .
, . , , .
, ++. exception, <stdexcept>. , new bad_alloc.
, .
( , , , , . .).
1. .. / C++.
/ .. . , 2002.
2. .. / C++. :
/ .., .. . , 2002.
3. .. / C++. - : / .., .. , 2002.
4. .. ++ Builder./ ... , 2004.
. 2
.. 4
. 4
. 6
this. 7
.. 8
. 11
. 12
. 12
.. 13
.. 14
. 14
. 15
.. 16
. 18
. 20
. 20
. 21
. 22
. 22
. 23
. 27
. 28
.. 30
.. 30
. 31
. 32
. 35
. 35
.. 37
++