C++/Language/try catch
Содержание
- 1 Catch all types of exceptions
- 2 Catch By Non Const Reference
- 3 Catch By Value
- 4 Catch char pointer type exception
- 5 Catch different types of exception
- 6 Catch exception from a function
- 7 Catch int type exception in a function
- 8 Catch more than one type of exceptions
- 9 Checking for a divide-by-zero exception.
- 10 Code block of try...catch
- 11 Finally catch all Exceptions
- 12 Localize a try/catch to a function.
- 13 Use multiple catch statements.
- 14 Using Multiple catch Statements
Catch all types of exceptions
<source lang="cpp">
- include <iostream>
using namespace std; void XHandler(int test){
try { if(test==0) throw test; if(test==1) throw "a"; if(test==2) throw 123.23; }catch(...){ cout << "Caught one." << endl; }
} int main(void){
cout << "Start: " << endl; XHandler(0); XHandler(1); XHandler(2); cout << "End";
}
</source>
Catch By Non Const Reference
<source lang="cpp">
- include <fstream>
- include <iostream>
- include <vector>
- include <string>
- 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 exception(); } while (istr >> temp) { dest.push_back(temp); }
} int main(int argc, char** argv) {
vector<int> myInts; const string fileName = "IntegerFile.txt"; try { readIntegerFile(fileName, myInts); } catch (exception& e) { cerr << "Unable to open file " << fileName << endl; exit (1); } for (size_t i = 0; i < myInts.size(); i++) { cout << myInts[i] << " "; } cout << endl; return (0);
}
</source>
Catch By Value
<source lang="cpp">
- include <fstream>
- include <iostream>
- include <vector>
- include <string>
- 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 exception(); } while (istr >> temp) { dest.push_back(temp); }
} int main(int argc, char** argv) {
vector<int> myInts; const string fileName = "IntegerFile.txt"; try { readIntegerFile(fileName, myInts); } catch (exception e) { cerr << "Unable to open file " << fileName << endl; exit (1); } for (size_t i = 0; i < myInts.size(); i++) { cout << myInts[i] << " "; } cout << endl; return (0);
}
</source>
Catch char pointer type exception
<source lang="cpp">
- include <iostream>
using namespace std; void XHandler(void)
{ try { throw "hello"; } catch(char *) { cout << "Caught char * inside XHandler." << endl; throw; } }
int main(void)
{ cout << "Start: " << endl; try { XHandler(); } catch(char *) { cout << "Caught char * inside main." << endl; } cout << "End"; } </source>
Catch different types of exception
<source lang="cpp">
- include <iostream>
using namespace std; void XHandler(int test) throw(){
if(test==0) throw test; if(test==1) throw "a"; if(test==2) throw 123.23;
} int main(void){
cout << "Start: " << endl; try{ XHandler(0); // try passing 1 and 2 for different responses }catch(int i){ cout << "Caught an integer." << endl; }catch(char c){ cout << "Caught a character." << endl; }catch(double d){ cout << "Caught a double." << endl; } cout << "End";
}
</source>
Catch exception from a function
<source lang="cpp">
- include <iostream>
using namespace std; void XHandler(int test) throw(int, char, double){
if(test==0) throw test; if(test==1) throw "a"; if(test==2) throw 123.23;
} int main(void){
cout << "Start: " << endl; try { XHandler(0); // try passing 1 and 2 for different responses } catch(int i) { cout << "Caught an integer." << endl; } catch(char c) { cout << "Caught a character." << endl; } catch(double d) { cout << "Caught a double." << endl; } cout << "End"; } </source>
Catch int type exception in a function
<source lang="cpp">
- include <iostream>
using namespace std; void XHandler(int test){
try { if(test) throw test; }catch(int i){ cout << "Caught exception #: " << i << endl; }
} int main(void){
cout << "Start: " << endl; XHandler(1); XHandler(2); XHandler(0); XHandler(3); cout << "End";
}
</source>
Catch more than one type of exceptions
<source lang="cpp">
- include <iostream>
using namespace std; void XHandler(int test){
try { if(test==0) throw test; if(test==1) throw "a"; if(test==2) throw 123.23; }catch(int i){ cout << "Caught an integer." << endl; }catch(...){ cout << "Caught one." << endl; }
} int main(void){
cout << "Start: " << endl; XHandler(0); XHandler(1); XHandler(2); cout << "End";
}
</source>
Checking for a divide-by-zero exception.
<source lang="cpp">
- include <iostream>
using std::cout; using std::cin; using std::endl; class DivideByZeroException { public:
DivideByZeroException() : message( "attempted to divide by zero" ) { } const char *what() const { return message; }
private:
const char *message;
}; double quotient( int numerator, int denominator ) {
if ( denominator == 0 ) throw DivideByZeroException(); return static_cast< double > ( numerator ) / denominator;
} int main() {
int number1, number2; double result; cout << "Enter two integers (end-of-file to end): "; while ( cin >> number1 >> number2 ) { try { result = quotient( number1, number2 ); cout << "The quotient is: " << result << endl; } catch ( DivideByZeroException ex ) { cout << "Exception occurred: " << ex.what() << "\n"; } cout << "\nEnter two integers (end-of-file to end): "; } cout << endl; return 0;
}
</source>
Code block of try...catch
<source lang="cpp">
- include <iostream>
using namespace std; int main(void){
cout << "Start" << endl; try { cout << "Inside try block." << endl; cout << "Still inside try block." << endl; }catch(int i){ cout << "Caught an exception--value is: " << endl; cout << i << endl; } cout << "End";
}
</source>
Finally catch all Exceptions
<source lang="cpp">
- include <iostream>
- include <stdlib.h>
using namespace std; void foo() {
int i, j; i = 14; j = 15;
} void call_foo() {
int k; k = 12; foo();
} void call_foo2() {
double x = 1.3; throw (x);
} int main() {
try { call_foo(); //foo exitted with i and j destroyed call_foo2(); } catch (char* message) { cerr << message << endl; exit(1); } catch(int n) { cout << "\ncaught it " << n << endl; } catch( ... ) { cerr << "THAT"S ALL FOLKS." << endl; abort(); }
}
</source>
Localize a try/catch to a function.
<source lang="cpp">
- include <iostream>
using namespace std;
void myFunction(int test) {
try{ if( test ) throw test; }catch(int i) { cout << "Caught Exception #: " << i << "\n"; }
} int main() {
cout << "Start\n"; myFunction(1); myFunction(2); myFunction(0); myFunction(3); cout << "End"; return 0;
}
</source>
Use multiple catch statements.
<source lang="cpp">
- include <iostream>
using namespace std;
void myFunction(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"; myFunction(1); myFunction(2); myFunction(0); myFunction(3); cout << "end"; return 0;
}
</source>
Using Multiple catch Statements
<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>