C++ 基础知识throw的用法


    throw 用于捕捉异常,将参数传递给catch,stdexcept文件中的异常类型范围很大,也可以直接传递错误值来处理。

#include<iostream>
#include<stdexcept>
#include<Windows.h>
using namespace std;
int main()
{
	int a, b;
	error1:try {
	cin >> a >> b;
	if (b == 0)
		throw runtime_error("b can't be 0.\n");
	}catch(runtime_error){
		cout << "Do you want to input again ?\n Enter y or n." << endl;
		char c;
		cin >> c;
		if (c == 'y')
			goto error1;
	}
	cout << a / b;
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42426141/article/details/80638455