Language/C & C++

Exception handling

Rogue 2022. 10. 14. 10:31
반응형
#include <iostream>

/* Define function for Rule 3. *//*
void except(int a, int b)
{
	if (b == 0)
	{
		throw b;
	}
	std::cout << "Division result :" << a << " / " << b << " = " << a / b << std::endl;
}
*/

int main(void)
{
	int a;
	int b;

	std::cout << "Please enter the two interger: ";
	std::cin >> a >> b;
	
	/*======================================================================*/

	/* Common  *//*
	std::cout << "Division result :" << a << " / " << b << " = " << a / b << std::endl;
	*/

	/*======================================================================*/

	/* Rule 1. *//*
	if (b == 0)
		std::cout << "Divisior cannot be zero" << std::endl;
	else
		std::cout << "Division result :" << a << " / " << b << " = " << a / b << std::endl;
	*/

	/*======================================================================*/

	/* Rule 2. (Recommend) *//*
	try
	{
		if (b == 0)
		{
			throw b;
		}
		std::cout << "Division result :" << a << " / " << b << " = " << a / b << std::endl;
	}
	catch (int exception)
	{
		std::cout << "Occured exception : Divisior cannot be " << b << std::endl;
	}
	*/
	
	/*======================================================================*/

	/* Rule 3. (Using function) *//*
	try
	{
		except(a, b);
	}
	catch (int exception)
	{
		std::cout << "Occured exception : Divisior cannot be " << b << std::endl;
	}
	*/

	/*======================================================================*/

	return 0;
}
반응형