C++细节笔记14

1. 基本的文件IO方式

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
	fstream fs;

	fs.open("1.txt", ios_base::in | ios_base::out);

	if (!fs.is_open())
	{
		cout << "打开文件失败!" << endl;
		return(-1);
	}
	int iNum = 55;
	char szBuf[] = "8866";
	int iHex = 0x5566;

	fs.write((char*)&iNum, sizeof(int))
		.write(szBuf, sizeof(szBuf))
		.write((char *)&iHex, sizeof(int));

	// fs的write和read不能混用,需要调用seekg回归起始位置
	fs.seekg(0, ios_base::beg);

	char szNewBuf[5] = { 0 };
	int iNewNum = 0;
	int iNewHex = 0;

	fs.read((char *)&iNewNum, sizeof(iNewNum))
		.read(szNewBuf, sizeof(szNewBuf))
		.read((char *)&iNewHex, sizeof(iNewHex));
	cout << iNewNum << " " << szNewBuf << " " << hex << iNewHex << endl;

	fs.close();

	system("pause");
	return(0);
}

2. 异常处理

  • 如果异常没有被catch处理(即try块后没有跟catch), 则编译器调用abort()函数并弹框提示后(提示对话框只有Debug会显示,Release不会)退出程序。
  • 异常处理中,类型必须严格匹配不存在隐式类型转换,多种类型需要多个catch来处理
  • throw之前创建的对象会调用其析构函数
  • 在构造中调用throw会导致析构不执行(因为可能构造未完成), 析构中调用throw直接调用abort()并弹出提示对话框(同样只有Debug版会)
  • 异常会随着调用链向上传递
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int Add(int iLeft, int iRight)
{
	int iNum = iLeft + iRight;
	if (iLeft > 0 && (iRight > 0) && (iNum <= 0))
	{
		throw 1;
	}

	return(iNum);
}

int Sub(int iLeft, int iRight)
{
	int iNum = iLeft - iRight;
	if ((iLeft < iRight) && (iNum > 0))
	{
		throw 2;
	}
	return(iNum);
}

int main()
{
	try
	{
		Add(0x7fffffff, 2);
	}
	catch (int iErr)
	{
		switch (iErr)
		{
		case 1:
			cout << "加法溢出" << endl;
			break;
		case 2:
			cout << "减法溢出" << endl;
			break;
		}
	}
	catch (...)
	{
		cout << "其他异常类型" << endl;
	}

	system("pause");
	return(0);
}

3. 异常类

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>

using namespace std;

class MyException
{
public:
	MyException(string strFileName, string strFuncName, size_t nLineNum, string strErr)
		: m_strFileName(strFileName), m_strFuncName(strFuncName), m_nLineNum(nLineNum), m_strErr(strErr)
	{

	}

	string Info() const
	{
		char szBuf[256] = { 0 };
		sprintf(szBuf, "%u", (unsigned)m_nLineNum);
		return(m_strFileName + " " + szBuf + " " + m_strFuncName + ": " + m_strErr);
	}
private:
	string m_strFileName;
	string m_strFuncName;
	size_t m_nLineNum;
	string m_strErr;
};

// 特定派生的某一种错误的异常类
class CMyExceptionOverflow : public MyException
{
	CMyExceptionOverflow(string strFileName, string strFuncName, size_t nLineNum)
		: MyException(strFileName, strFuncName, nLineNum, "溢出错误")
	{

	}
};

void ExceptionThrow()
{
	throw MyException(__FILE__, __FUNCTION__, __LINE__, "测试错误信息");
}

int main()
{
	try
	{
		ExceptionThrow();
	}
	catch (MyException &e)
	{
		cout << e.Info() << endl;
	}

	system("pause");
	return(0);
}

(完)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值