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 293 views
1 answer 236 views
236 views asked Oct 8, 2022 by avibootz
1 answer 276 views
276 views asked Jul 11, 2022 by avibootz
1 answer 325 views
325 views asked Jun 4, 2021 by avibootz
1 answer 292 views
1 answer 274 views
274 views asked Apr 3, 2021 by avibootz
...