}
, char *, . .
#include <iostream>
using namespace std;
void int_exception(int i)
{ if(i>100) throw 1; // int
}
void string_exception()
{ throw "Error"; // char *
}
int main()
{ try{ //
int_exception(99); // int
string_exception(); // char *
}
catch(int){
cout<<" int"<<endl;
}
catch(...){
cout<<" "<<endl;
}
}
:
, . .
, , , , , :
- , . . , , ;
- ;
- ;
- . , , ;
- , , .
try, . , .
3 4 : , . , , , , ( ).
. , ; , .
|
|
#include<iostream.h>
using namespace std;
class A {};
class B {};
class C: public A, public B {};
void f(int i)
{ if(i) throw C(); //
// ѻ
else throw new C; //
// ѻ
}
int main()
{ int i;
try{
cin>>i;
f(i);
}
catch(A) {
cout<<"A handler";
}
catch(B&) {
cout<<"B& handler";
}
catch(C) {
cout<<"C handler";
}
catch(C*) {
cout<<"C* handler";
}
catch(A*) {
cout<<"A* handler";
}
catch(void*) {
cout<<"void* handler";
}
}
A, B& C, , . , , A* C*. void*. void* , . :
#include <iostream.h>
using namespace std;
class base {};
class derived: public base{};
void fun()
{ derived obj;
base &a = obj;
throw a;
}
int main()
{ try{ fun();
}
catch(derived){ cout<<derived<<endl;
}
catch(base) { cout<<base<<endl;
}
}
:
base
, base, a base, derived. , a base, derived.
, , () ( catch).
throw. throw. , , .. . , . , .
, , , . throw , , , . . , , , .
, . . , , .
|
|