C++ Tutorial/Exceptions/try catch

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

A simple exception handling example

#include <iostream>
using namespace std;
int main()
{
  cout << "Start\n";
  try {
    cout << "Inside try block\n";
    throw 1;                          // throw an error
    cout << "This will not execute";
  }
  catch (int i) {                       // catch an error
    cout << "Caught an exception -- value is: ";
    cout << i << "\n";
  }
  cout << "End";
  return 0;
}
Start
Inside try block
Caught an exception -- value is: 1
End"

A try block can be localized to a function

#include <iostream> 
using namespace std; 
 
void f(int test) 
{ 
  try{ 
    if(test) throw test; 
  } 
  catch(int i) { 
    cout << "Caught One!  Ex. #: " << i << "\n"; 
  } 
} 
 
int main() 
{ 
  cout << "start\n"; 
 
  f(1); 
  f(2); 
  f(0); 
  f(3); 
 
  cout << "end"; 
 
  return 0; 
}
start
Caught One!  Ex. #: 1
Caught One!  Ex. #: 2
Caught One!  Ex. #: 3
end

Catch all exceptions

#include <iostream> 
using namespace std; 
 
void f(int test) 
{ 
  try{ 
    if(test==0) throw test; // throw int 
    if(test==1) throw "a"; // throw char 
    if(test==2) throw 123.23; // throw double 
  } 
  catch(...) { // catch all exceptions 
    cout << "Caught One!\n"; 
  } 
} 
 
int main() 
{ 
  f(0); 
  f(1); 
  f(2); 
 
  return 0; 
}
Caught One!
Caught One!
Caught One!

Catch "char *" exception

#include <iostream.h>
int main () {
  try
  {
      throw "Out of range";
  }
  catch (char * str)
  {
    cout << "Exception: " << str << endl;
  }
  return 0;
}
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application"s support team for more information.

Catching derived classes

#include <iostream>
using namespace std;
class B {
};
class D: public B {
};
int main()
{
  D derived;
  try {
    throw derived;
  }
  catch(B b) {
    cout << "Caught a base class.\n";
  }
  catch(D d) {
    cout << "This won"t execute.\n";
  }
  return 0;
}

Multiple catch blocks

#include <iostream.h>
int main () {
  try
  {
    char * mystring;
    mystring = new char [10];
    if (mystring == NULL) 
       throw "Allocation failure";
    
    for (int n=0; n<=100; n++)
    {
      if (n>9) 
         throw n;
      mystring[n]="a";
      
    }
  }
  catch (int i)
  {
    cout << "index " << i << " is out of range" << endl;
  }
  catch (char * str)
  {
    cout << "Exception: " << str << endl;
  }
  return 0;
}
index 10 is out of range

Throwing an exception from a function called from within a try block

#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; 
}
start
Inside try block
Inside f, test is: 0
Inside f, test is: 1
Caught an exception -- value is: 1
end

try catch block without exception being thrown

#include <iostream>
using namespace std;
int main()
{
  cout << "Start\n";
  try { 
    cout << "Inside try block\n";
    cout << "Still inside try block\n";
  }
  catch (int i) {
    cout << "Caught an exception -- value is: ";
    cout << i << "\n";
  }
  cout << "End";
  return 0;
}
Start
Inside try block
Still inside try block
End"

Use catch(...) as a default

#include <iostream>
using namespace std;
void f(int val)
{
  try{
    if(val==0) 
       throw val;
    if(val==1) 
       throw "a";
    if(val==2) 
       throw 123.23;
  }
  catch(int i) {                      // catch an int exception
    cout << "Caught an integer\n";
  }
  catch(...) {                        // catch all other exceptions
    cout << "Caught One!\n";
  }
}
int main()
{
  f(0);
  f(1);
  f(2);
  return 0;
}
Caught an integer
Caught One!
Caught One!

Use multiple catch statements

#include <iostream> 
using namespace std; 
 
void f(int test) 
{ 
  try{ 
    if(test) 
       throw test;            // throw int 
    else 
       throw "Value is zero"; // throw char * 
  } 
  catch(int i) { 
    cout << "Caught One!  Ex. #: " << i << "\n"; 
  } 
  catch(char *str) { 
    cout << "Caught a string: "; 
    cout << str << "\n"; 
  } 
} 
 
int main() 
{ 
  cout << "start\n"; 
 
  f(1); 
  f(2); 
  f(0); 
  f(3); 
 
  cout << "end"; 
 
  return 0; 
}
start
Caught One!  Ex. #: 1
Caught One!  Ex. #: 2
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application"s support team for more information.