C++/Language/Throw
Содержание
- 1 An exception can be thrown from outside the try block
- 2 checks for divide-by-zero and throw exception
- 3 Demonstrating stack unwinding.
- 4 Demonstration of rethrowing an exception.
- 5 Example of "rethrowing" an exception.
- 6 Restricting function throw types.
- 7 throw an integer out
- 8 Throw Char Star
- 9 throw different types of exceptions with if statement
- 10 throw exception out of function
- 11 Throwing Exceptions
- 12 Throwing Exceptions out of nested function calls
- 13 Throwing Multiple Basic
- 14 Throwing Two Types
- 15 Throw Int out when reading integer file
An exception can be thrown from outside the try block
#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;
}
checks for divide-by-zero and throw exception
#include <iostream>
using namespace std;
void divide(double a, double b)
{
try {
if(!b) throw b; // checks for divide-by-zero
cout << "Result: " << a/b << endl;
}
catch(double b) {
cout << "Can"t divide by zero." << endl;
}
}
int main(void)
{
double i, j;
do {
cout << "Enter numerator (0 to stop):" << endl;
cin >> i;
cout << "Enter denominator: " << endl;
cin >> j;
divide (i,j);
}
while (i !=0);
}
Demonstrating stack unwinding.
#include <iostream>
#include <stdexcept>
using std::cout;
using std::endl;
using std::runtime_error;
void function3() throw ( runtime_error )
{
throw runtime_error( "runtime_error in function3" );
}
void function2() throw ( runtime_error )
{
function3();
}
void function1() throw ( runtime_error )
{
function2();
}
int main()
{
try {
function1();
}catch ( runtime_error e ){
cout << "Exception occurred: " << e.what() << endl;
}
return 0;
}
Demonstration of rethrowing an exception.
#include <iostream>
using std::cout;
using std::endl;
#include <exception>
using std::exception;
void throwException()
{
try {
throw exception();
}
catch( exception e )
{
cout << "Exception handled in function throwException\n";
throw;
}
cout << "This also should not print\n";
}
int main()
{
try {
throwException();
cout << "This should not print\n";
}catch ( exception e ){
cout << "Exception handled in main\n";
}
return 0;
}
Example of "rethrowing" an exception.
#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
} // myFunction
}
int main()
{
cout << "Start\n";
try{
myFunction();
}
catch(const char *) {
cout << "Caught char * inside main\n";
}
cout << "End";
return 0;
}
Restricting function throw types.
#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;
}
throw an integer out
#include <iostream>
using namespace std;
int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << endl;
}
return 0;
}
Throw Char Star
#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 "Unable to open file";
}
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 (const char* e) {
cerr << e << endl;
exit (1);
}
for (size_t i = 0; i < myInts.size(); i++) {
cout << myInts[i] << " ";
}
cout << endl;
return (0);
}
throw different types of exceptions with if statement
#include <iostream>
using namespace std;
void XHandler(int test){
try {
if(test==0) throw test;
if(test==1) throw "String";
if(test==2) throw 123.23;
}catch(int i){
cout << "Caught exception #: " << i << endl;
}catch(char *str){
cout << "Caught string exception: " << str << endl;
}catch(double d){
cout << "Caught exception #: " << d << endl;
}
}
int main(void){
cout << "Start: " << endl;
XHandler(0);
XHandler(1);
XHandler(2);
cout << "End";
}
throw exception out of function
#include <iostream>
using namespace std;
void XHandler(int test){
cout << "Inside XHandler, test is:" << test << endl;
if(test) throw test;
}
int main(void){
cout << "Start: " << endl;
try {
cout << "Inside try block." << endl;
XHandler(1);
XHandler(2);
XHandler(0);
}
catch(int i) {
cout << "Caught an exception. Value is: ";
cout << i << endl;
}
cout << "End";
}
Throwing Exceptions
#include <iostream>
using namespace std;
void foo()
{
int i;
i = -15;
throw i;
}
int main()
{
try {
foo();
}
catch(int n)
{ cerr << "exception caught\n " << n << endl; }
}
Throwing Exceptions out of nested function calls
#include <iostream>
using namespace std;
void foo()
{
int i, j;
i = 14;
j = 15;
throw i;
}
void call_foo()
{
int k;
k = 12;
foo();
}
int main()
{
try {
call_foo();
}
catch(int n) { cout << "\ncaught it " << n << endl; }
}
Throwing Multiple Basic
#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);
}
if (istr.eof()) {
istr.close();
} else {
istr.close();
throw exception();
}
}
int main(int argc, char** argv)
{
vector<int> myInts;
const string fileName = "IntegerFile.txt";
try {
readIntegerFile(fileName, myInts);
} catch (const exception& e) {
cout << "Unable either to open or to read " << fileName << endl;
exit (1);
}
for (size_t i = 0; i < myInts.size(); i++) {
cout << myInts[i] << " ";
}
cout << endl;
return (0);
}
Throwing Two Types
#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 (const invalid_argument& e) {
cerr << "Unable to open file " << fileName << endl;
exit (1);
} catch (const runtime_error& e) {
cerr << "Error reading file " << fileName << endl;
exit (1);
}
for (size_t i = 0; i < myInts.size(); i++) {
cout << myInts[i] << " ";
}
cout << endl;
return (0);
}
Throw Int out when reading integer file
#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 5;
}
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 (int 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);
}