() - , ( ).
. .
C++ ( ), , .
- .
:
,
, , , , . , ,
, .
:
, , , () ,
, ,
, terminate, abort, . .
, . .
- , .
- - . .
, , , , .
(, ):
try {
...
}
try- .
:
throw [ ];
() . , . () .
, , try-. . , throw .
|
|
( ):
1. , - (, ).
catch( ){... /* */ }, - .
2. , .
catch(){... /* */ }
3. .
catch(...){.. /* */ }
, , .
try-. .
:
catch (int i){
... // int
}
catch (const char *){
... // const char*
}
catch (Overflow){
... // Overflow
}
catch(...){
... //
}
, . try- , , , .
throw C++ :
throw ( , );
( , ). , ;
.
, , throw:
, catch ( , const , & const &, җ );
catch ( public). ( ).
, catch. void ( ).
:
#include <fstream.h>
class Hello{
// ,
public:
Hello(){cout << "Hello!" << endl;}
~Hello(){cout << "Bye!" << endl;}
};
void f1(){
ifstream ifs("\\INVALID\\FILE\\NAME"); //
if (!ifs){
cout << " " << endl;
throw " ";
}
}
void f2(){
Hello ; //
f1(); // .
}
int main () {
try{
cout << " try-" << endl;
f2();
cout << " try-" << ndl;
|
|
}
catch (int i){
cout << " int, - " << i << endl;
return -1;
}
catch (const char * p){
cout << " const char*, - " << p << endl;
return - 1;
}
catch(...){
cout << " " << endl;
return -1;
}
return 0; //
}
:
try-
Hello!
Bye!
const char *, -
, f1 , main. try- .
. , .
. , - . , .
, . - , .
, . public, ( , , public).
, . throw, :
void fl() throw (int, const char*){ /* */ }
void f2() throw (Oops*){ /* */ }
fl int const char*, f2 - Oops .
throw , . , :
void f() throw (){
// ,
}
, , , ( , ).
unexpected, terminate ( 2.3). set_unexpected , terminate .
terminate abort, . set_terminate , abort . set_unexpected set_terminate <exception>.
2.3 - .
. , , .