How to use try catch inside a function in C++

1 Answer

0 votes
#include <iostream>

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

void function(int n)
{
	try {
		if (n)
			throw n;
	}
	catch (int __n) {
		cout << "catch n = " << __n << endl;
	}
}

int main()
{
	function(99);
	function(0);
	function(13);

	return 0;
}




/*
run:

catch n = 99
catch n = 13

*/

 



answered Jun 2, 2018 by avibootz

Related questions

2 answers 194 views
1 answer 146 views
146 views asked Oct 8, 2022 by avibootz
1 answer 177 views
177 views asked Jul 11, 2022 by avibootz
1 answer 237 views
237 views asked Jun 4, 2021 by avibootz
1 answer 212 views
1 answer 213 views
213 views asked Apr 3, 2021 by avibootz
...