No.2 抛出异常

No.2 抛出异常

虽然学了一年的C++,但仍然不常用throw try catch什么的,今天写一点,毕竟是真的实际有用。

1.返回类型为int

#include<iostream>
using namespace std;

int abc(int a, int b, int c) {
	if (a <= 0 && b > 0 && c > 0) {
		throw 1;
	}
	else if (a > 0 && b <= 0 && c > 0) {      //仅列出来了两个参数错误的例子
		throw 2;
	}
	return a + b*c;
}

int main() {
	int a, b, c;
	while (cin >> a >> b >> c) {
		try {
			cout << "The anwser is " << abc(a, b, c) << endl;
		}
		catch (int x) {
			cout << "第" << x << "个参数错误!" << endl;
		}
	}
	return 0;
}

运行结果:
Ctrl+z结束输入


2.返回类型为char *

#include<iostream>
#include<string>
using namespace std;

char* abc(int a, int b, int c) {
	if (a <= 0 && b > 0 && c > 0) {
		throw "WRONG 1 !";
	}
	else if (a > 0 && b <= 0 && c > 0) {      //仅列出来了两个参数错误的例子
		throw "WRONG 2 !";
	}
	int s = a + b*c;
	char * str;	
	str = new char[50];
	char * ch = {"This answer is "};
	for (int i = 0; i <50; i++) {
		str[i] = 0;
	}
	int i0 = 0;
	for (;;) {
		if (s == 0) {
			break;
		}
		str[i0] = s % 10+'0';
		s /= 10;
		i0++;
	}
	char *str_n = new char[strlen(ch) + strlen(str) + 1];
	strcpy(str_n, ch);
	strcat(str_n, str);
	return str_n;
}

int main() {
	int a, b, c;
	while (cin >> a >> b >> c) {
		try {
			cout <<  abc(a, b, c) << endl;
		}
		catch (char* x) {
			cout << x<< endl;
		}
	}
	return 0;
}

运行结果:第二次结果


3.多返回类型捕捉

#include<iostream>
#include<string>
using namespace std;

char* abc2(int a, int b, int c) {
	if (a <= 0 && b > 0 && c > 0) {
		throw "WRONG 1 !";
	}
	else if (a > 0 && b <= 0 && c > 0) {      //仅列出来了两个参数错误的例子
		throw "WRONG 2 !";
	}
	int s = a + b*c;
	char * str;	
	str = new char[50];
	char * ch = {"This answer is "};
	for (int i = 0; i <50; i++) {
		str[i] = 0;
	}
	int i0 = 0;
	for (;;) {
		if (s == 0) {
			break;
		}
		str[i0] = s % 10+'0';
		s /= 10;
		i0++;
	}
	char *str_n = new char[strlen(ch) + strlen(str) + 1];
	strcpy(str_n, ch);
	strcat(str_n, str);
	return str_n;
}
int abc1(int a, int b, int c) {
	if (a <= 0 && b > 0 && c > 0) {
		throw 1;
	}
	else if (a > 0 && b <= 0 && c > 0) {      //仅列出来了两个参数错误的例子
		throw 2;
	}
	return a + b*c;
}

int main() {
	int a, b, c;
	while (cin >> a >> b >> c) {
		try {
			cout << "The anwser is " << abc1(a, b, c) << endl;
			cout <<  abc2(a, b, c) << endl;
		}
		catch (char* x) {
			cout << x << endl;
		}
		catch (int x) {
			cout << "第" << x << "个参数错误!" << endl;     //观察输出结果
		}
	}
	return 0;
}

运行结果:
在这里插入图片描述也就是说,会抛出第一个错误,然后停止运行(abc2并没有输出结果)。

MyBatis在执行select语句时,如果没有在Mapper映射文件中显示声明返回值类型,会抛出异常,指出"A query was run and no Result Maps were found for the Mapped Statement"。\[3\]这意味着在Mapper映射文件中必须指定返回值类型,且该返回值类型需要与Mapper接口中声明的返回值类型一致,可以是Integer、Long、int或long,但不能返回Boolean类型。\[3\]对于insert、update、delete语句,MyBatis会使用rowCountResult方法对返回值进行转换,如果返回的值大于0,则返回True,否则返回False。\[1\]对于query函数,如果工程配置的defaultExecutorType是REUSE,MyBatis会使用ReuseExecutor的doQuery方法进行查询,但无法确定list返回是否为null。\[2\] #### 引用[.reference_title] - *1* *3* [MyBatis中Mapper的返回值类型](https://blog.csdn.net/tantiao666/article/details/106344829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [探索Mybatis中Mapper默认返回值](https://blog.csdn.net/zhuqiuhui/article/details/87899146)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值