C++ Tutorial/Exceptions/function and Exception

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Localize a try/catch to a function

<source lang="cpp">#include <iostream> using namespace std; void f(int test) {

 try{
   if(test) throw test;
 }
 catch(int i) {
   cout << "Caught Exception #: " << i << "\n";
 }

} int main() {

 cout << "Start\n";
 f(1);
 f(2);
 f(0);
 f(3);
 cout << "End";
 return 0;

}</source>

Start
Caught Exception #: 1
Caught Exception #: 2
Caught Exception #: 3
End"

Restricting function throw types

<source lang="cpp">#include <iostream> using namespace std; // only throw ints, chars, and doubles void f(int val) throw(int, char, double) {

 if(val==0) 
    throw val;
 if(val==1) 
    throw "a";
 if(val==2) 
    throw 123.23;

} int main() {

 try{
   f(0); // also, try passing 1 and 2 to f()
 }
 catch(int i) {
   cout << "Caught an integer\n";
 }
 catch(char c) { 
   cout << "Caught char\n";
 }
 catch(double d) { 
   cout << "Caught double\n";
 }
 return 0;

}</source>

Caught an integer

This function can throw NO exceptions!

<source lang="cpp">#include <iostream> using namespace std; void f(int val) throw(); int main() {

 try{
   f(0); // also, try passing 1 and 2 to f()
 }
 catch(int i) {
   cout << "Caught an integer\n";
 }
 catch(char c) {
   cout << "Caught char\n";
 }
 catch(double d) {
   cout << "Caught double\n";
 }
 return 0;

}

// This function can throw NO exceptions! void f(int val) throw() {

 cout << "f";

}</source>

f"

Throwing an exception from a function outside the try block

<source lang="cpp">#include <iostream> using namespace std; void f(int test) {

 cout << "Inside f, test is: " << test << "\n";
 if(test) throw test;

} int main() {

 cout << "Start\n";
 try { // start a try block
   cout << "Inside try block\n";
   f(0);
   f(1);
   f(2);
 }
 catch (int i) { // catch an error
   cout << "Caught an exception -- value is: ";
   cout << i << "\n";
 }
 cout << "End";
 return 0;

}</source>

Start
Inside try block
Inside f, test is: 0
Inside f, test is: 1
Caught an exception -- value is: 1
End"