鼠标滚动在win10 设置无效;右键菜单会自己弹出。
1194
2022-05-30
throw 是C++中的关键字,用来抛出异常。如果不使用 throw 关键字,try 就什么也捕获不到;上节提到的 at() 函数在内部也使用了 throw 关键字来抛出异常。
throw 既可以用在标准库中,也可以用在自定义的函数中,抛出我们期望的异常。throw 关键字语法为:
throw exceptionData;
#include
#include
using namespace std;
char get_char(const string &, int);
int main(){
string str = "c plus plus";
try{
cout<
cout<
}catch(int e){
if(e==1){
cout<<"Index underflow!"<
}else if(e==2){
cout<<"Index overflow!"<
}
}
return 0;
}
char get_char(const string &str, int index){
int len = str.length();
if(index < 0)
throw 1;
if(index >= len)
throw 2;
return str[index];
}
不被建议的用法
double func (char param) throw (int);
double func (char param) throw (int, char, exception);
double func (char param) throw ();
#include
#include
using namespace std;
char get_char(const string &, int) throw(char, exception);
int main(){
string str = "c plus plus";
try{
cout<
cout<
}catch(int e){
if(e==1){
cout<<"Index underflow!"<
}else if(e==2){
cout<<"Index overflow!"<
}
}
return 0;
}
char get_char(const string &str, int index) throw(char, exception){
int len = str.length();
if(index < 0)
throw 1;
if(index >= len)
throw 2;
return str[index];
}
C++
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。