NDK14_C++基础:异常

NDK开发汇总

一 普通异常

void main() {
	try {
		int age = 300;
		if (age > 200) {
			throw 9;
		}
	}
	catch (int a) {
		cout << "int 异常" << a<< endl;
	}

	system("pause");
}

结果:int 异常9

可以是其他存在的类型

void test1()
{
	throw "测试!";
}

void test2()
{
	throw exception("测试");
}

try {
	test1();
}
catch (const char *m) {
	cout << m << endl;
}
try {
	test2();
}
catch (exception  &e) {
	cout << e.what() << endl;
}

二 多种类型的

(char * 先在 vs项目属性中设置符合模式)

void main() {
	try {
		int age = 300;
		if (age > 200) {
			throw "xxx";
		}
	}
	catch (int a) {
		cout << "int 异常" << a<< endl;
	}
	catch (char * b) {
		cout << "char *b" << b << endl;
	}
	
	system("pause");
}

结果:char *bxxx

三 标准异常

类似于java中javaNullPointerException

class NullPointerException :public exception {

public :
	NullPointerException(char * msg) :exception(msg){

	}
};


void main() {
	try {
		int age = 300;
		if (age > 200) {
			throw NullPointerException("pgone");
		}
	}
	catch (int a) {
		cout << "int 异常" << a << endl;
	}
	catch (char * b) {
		cout << "char *b" << b << endl;
	}
	catch (NullPointerException e) {
		cout << "NullPointerException: " <<e.what() << endl;
	}
	
	system("pause");
}

结果:NullPointerException: pgone

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值