C++/Development/Exception
Содержание
- 1 An exception can be thrown from outside the try block
- 2 A try/catch can be inside a function other than main().
- 3 Catch exception: int i
- 4 Different types of exceptions can be caught.
- 5 Handle exceptions thrown by new.
- 6 Matching Any Exception
- 7 No Exception Handling
- 8 Restricting function throw types.
- 9 Rethrowing an Exception
- 10 Terminate Handler
- 11 Throw and catch an exception inside a function
- 12 Uses catch(...) to catch all exceptions
An exception can be thrown from outside the try block
<source lang="cpp">
- include <iostream>
using namespace std; void myFunction(int test) {
cout << "Inside myFunction, test is: " << test << "\n"; if(test) throw test;
} int main() {
cout << "Start\n"; try { cout << "Inside try block\n"; myFunction(0); myFunction(1); myFunction(2); } catch (int i) { cout << "Caught an exception -- value is: "; cout << i << "\n"; } cout << "End"; return 0;
}
</source>
A try/catch can be inside a function other than main().
<source lang="cpp">
- include <iostream>
using namespace std;
void myFunction(int test) {
try{ if(test) throw test; } catch(int i) { cout << "Caught One! Ex. #: " << i << "\n"; }
} int main() {
cout << "start\n"; myFunction(1); myFunction(2); myFunction(0); myFunction(3); cout << "end"; return 0;
}
</source>
Catch exception: int i
<source lang="cpp">
- 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) { // catch an error cout << "Caught an exception and value is: "; cout << i << endl; } cout << "End"; return 0;
}
</source>
Different types of exceptions can be caught.
<source lang="cpp">
- include <iostream>
using namespace std;
void myFunction(int test) {
try{ if(test) throw test; else throw "Value is zero"; }catch(int i) { cout << "Caught Exception #: " << i << "\n"; }catch(const char *str) { cout << "Caught a string: "; cout << str << "\n"; }
} int main() {
cout << "Start\n"; myFunction(1); myFunction(2); myFunction(0); myFunction(3); cout << "End"; return 0;
}
</source>
Handle exceptions thrown by new.
<source lang="cpp">
- include <iostream>
- include <new>
using namespace std; int main() {
int *p, i; try { p = new int[32]; // allocate memory for 32-element int array } catch (bad_alloc xa) { cout << "Allocation failure.\n"; return 1; } for(i = 0; i <32; i++) p[i] = i; for(i = 0; i <32; i++) cout << p[i] << " "; delete [] p; // free the memory return 0;
}
</source>
Matching Any Exception
<source lang="cpp">
- include <fstream>
- include <iostream>
- include <vector>
- include <string>
- include <stdexcept>
using namespace std; void readIntegerFile(const string& fileName, vector<int>& dest) {
ifstream istr; int temp; istr.open(fileName.c_str()); if (istr.fail()) { throw invalid_argument(""); } while (istr >> temp) { dest.push_back(temp); } if (istr.eof()) { istr.close(); } else { istr.close(); throw runtime_error(""); }
} int main(int argc, char** argv) {
vector<int> myInts; const string fileName = "IntegerFile.txt"; try { readIntegerFile(fileName, myInts); } catch (...) { cerr << "Error reading or opening file " << fileName << endl; exit (1); } for (size_t i = 0; i < myInts.size(); i++) { cout << myInts[i] << " "; } cout << endl; return (0);
}
</source>
No Exception Handling
<source lang="cpp">
- include <fstream>
- include <iostream>
- include <vector>
- include <string>
using namespace std; void readIntegerFile(const string& fileName, vector<int>& dest) {
ifstream istr; int temp; istr.open(fileName.c_str()); while (istr >> temp) { dest.push_back(temp); }
} int main(int argc, char** argv) {
vector<int> myInts; const string fileName = "IntegerFile.txt"; readIntegerFile(fileName, myInts); for (size_t i = 0; i < myInts.size(); i++) { cout << myInts[i] << " "; } cout << endl; return (0);
}
</source>
Restricting function throw types.
<source lang="cpp">
- include <iostream>
using namespace std;
void myFunction(int test) throw(int, char, double) {
if(test==0) throw test; // throw int if(test==1) throw "a"; // throw char if(test==2) throw 333.23; // throw double
} int main() {
cout << "start\n"; try{ myFunction(0); } catch(int i) { cout << "Caught an integer\n"; } catch(char c) { cout << "Caught char\n"; } catch(double d) { cout << "Caught double\n"; } try{ myFunction(1); } catch(int i) { cout << "Caught an integer\n"; } catch(char c) { cout << "Caught char\n"; } catch(double d) { cout << "Caught double\n"; } try{ myFunction(2); } catch(int i) { cout << "Caught an integer\n"; } catch(char c) { cout << "Caught char\n"; } catch(double d) { cout << "Caught double\n"; }
cout << "end"; return 0;
}
</source>
Rethrowing an Exception
<source lang="cpp">
- include <iostream>
using namespace std; void myFunction() {
try { throw "hello"; // throw a char * } catch(const char *) { // catch a char * cout << "Caught char * inside myFunction\n"; throw ; // rethrow char * out of function }
} int main() {
cout << "Start\n"; try{ myFunction(); } catch(const char *) { cout << "Caught char * inside main\n"; } cout << "End"; return 0;
}
</source>
Terminate Handler
<source lang="cpp">
- include <fstream>
- include <iostream>
- include <vector>
- include <string>
- include <stdexcept>
- include <exception>
using namespace std; void readIntegerFile(const string& fileName, vector<int>& dest) {
ifstream istr; int temp; istr.open(fileName.c_str()); if (istr.fail()) { throw invalid_argument(""); } while (istr >> temp) { dest.push_back(temp); } if (istr.eof()) { istr.close(); } else { istr.close(); throw runtime_error(""); }
} void myTerminate() {
cout << "Uncaught exception!\n"; exit(1);
} int main(int argc, char** argv) {
vector<int> myInts; const string fileName = "IntegerFile.txt"; set_terminate(myTerminate); readIntegerFile(fileName, myInts); for (size_t i = 0; i < myInts.size(); i++) { cerr << myInts[i] << " "; } cout << endl; return (0);
}
</source>
Throw and catch an exception inside a function
<source lang="cpp">
- include <iostream>
- include <cstdlib>
using namespace std; double divide(double a, double b) {
try { if(!b) throw(b); } catch(double) { cout << "Cannot divide by zero.\n"; exit(1); } return a/b;
} int main() {
cout << divide(10.0, 2.5) << endl; cout << divide(10.0, 0.0); return 0;
}
</source>
Uses catch(...) to catch all exceptions
<source lang="cpp">
- include <iostream>
using namespace std; void myFunction(int test) {
try{ if(test==0) // throw int throw test; if(test==1) // throw char throw "a"; if(test==2) // throw double throw 123.23; } catch(int i) { // catch an int exception cout << "Caught " << i << "\n"; } catch(...) { // catch all other exceptions cout << "Caught One!\n"; }
} int main() {
cout << "start\n"; myFunction(0); myFunction(1); myFunction(2); cout << "end"; return 0;
}
</source>