Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to throw different types from function in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

void function(int n) throw(int, char, double) {
	if (n == 0)
		throw n;
	if (n == 1)
		throw 'z';
	if (n == 2)
		throw 3.14;
}

int main()
{
	int i = 0;
	try {
		function(i);
	}
	catch (int i) {
		cout << "i = " << i << " catch (int i)" << endl;
	}
	catch (char ch) {
		cout << "i = " << i << " catch (char ch)" << endl;
	}
	catch (double d) {
		cout << "i = " << i << " catch (double d)" << endl;
	}

	i = 1;
	try {
		function(i);
	}
	catch (int i) {
		cout << "i = " << i << " catch (int i)" << endl;
	}
	catch (char ch) {
		cout << "i = " << i << " catch (char ch)" << endl;
	}
	catch (double d) {
		cout << "i = " << i << " catch (double d)" << endl;
	}

	i = 2;
	try {
		function(i);
	}
	catch (int i) {
		cout << "i = " << i << " catch (int i)" << endl;
	}
	catch (char ch) {
		cout << "i = " << i << " catch (char ch)" << endl;
	}
	catch (double d) {
		cout << "i = " << i << " catch (double d)" << endl;
	}

	return 0;
}


/*
run:

i = 0 catch (int i)
i = 1 catch (char ch)
i = 2 catch (double d)

*/

 



answered Jun 3, 2018 by avibootz
edited Jun 4, 2018 by avibootz
...