p13_3.cpp

来自「相当丰富的C++源码」· C++ 代码 · 共 25 行

CPP
25
字号
/****************************************************
* 程序名:p13_3.cpp                                *
* 功能: 标准异常类的使用                          *
****************************************************/
# include <iostream>
//# include <new>      //在Visual C++6.0中可以不包含
# include <string>
//# include <stdexcept>  //在Visual C++6.0中可以不包含
using namespace std;
void main() {
	string * S;
	try 
	{
		S=new string("ABCD");  //可能抛出bad_alloc异常
		cout<<S->substr(5,2);  //可能抛出out_of_range异常
	}
    catch(bad_alloc& NoMemory)
	{
		cout<<"Exception occurred: "<<NoMemory.what()<<endl;
	}
	catch(out_of_range& OutOfRange)
	{
		cout<<"Exception occurred: "<<OutOfRange.what()<<endl;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?