目录

第一章 C++回顾

函数与参数

1.交换两个整数的不正确代码。

异常

10.抛出并捕捉整型异常。


第一章 C++回顾

函数与参数

1.交换两个整数的不正确代码。

//test_1
void swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
}
void swap2(int& x,int& y)
{
int temp=x;
x=y;
y=temp;
}
void test_1()
{
int x=3,y=5;
swap(x,y);//error C2668: “swap”: 对重载函数的调用不明确.将void swap(int& x,int& y)改成void swap2(int& x,int& y)
cout<<x<<y<<endl;//35
int& a=x,b=y;//这里b是int。传值参数。int& a=3,&b=y;//这里b是int&。引用参数
cout<<a<<b<<endl;//35
swap2(a,b);
cout<<x<<y<<endl; //55,只有a改变了。
}

异常

10.抛出并捕捉整型异常。

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;
} void test_10()
{
try
{
cout<< abc(2,0,2)<<endl;
cout<< abc(-2,0,-2)<<endl;
cout<< abc(0,0,0)<<endl;
} catch(exception& e)
{
cout<<"aa "<<endl;
} catch(int e)
{
if (e==2)
{
cout<<"e==2 "<<endl;
} if (e==1)
{
cout<<"e==1 "<<endl;
}
} catch(...)
{
cout<<"..."<<endl;
}
}

输出

如果把catch(..)放在最前面会报错。error C2311: “int”: 在行 41 上被“...”捕获

因为这是捕获所有的,所以一般放最后。

	catch(...)
{
cout<<"..."<<endl;
system("pause");
return 1;
}
catch(int e)
{ if (e==2)
{
cout<<"e==2 "<<endl;
} if (e==1)
{
cout<<"e==1 "<<endl;
} system("pause");
return 1;
}

最新文章

  1. 用sublime写出的第一个网页
  2. 素数筛 uva 543
  3. 想抛就抛:Application_Error中统一处理ajax请求执行中抛出的异常
  4. Java NIO 概述
  5. Oracle Database 11g express edition
  6. Windows 2003 FastCgi安装环境
  7. 使用 Android Studio 跑新浪微博SDK Demo遇到的问题及解决
  8. ASP.NET树形控件TreeView的递归绑定
  9. POJ 1182 (经典食物链 /并查集扩展)
  10. 再回首,Java温故知新(十一):Java反射
  11. TreeView绑定无限层级关系类
  12. 8.4 sikuli 集成进eclipse 报错:Unsupported major.minor version 51.0
  13. 把Ubuntu用户目录下的目录名改成英文
  14. LinkQueue(链队列)
  15. 【Matlab&amp;Mathematica】对三维空间上的点进行椭圆拟合
  16. SonarQube 集成 GitLabCI
  17. SQL获取当前时间月份为两位数
  18. TreeSet多字段排序
  19. Weblogic环境(JSP)文件下载问题(下载的文件与原文件大小不一致问题)
  20. C# 调节图片亮度

热门文章

  1. 18.使用for循环计算+1-3+5-7+9-11+13...99的结果
  2. NOI2015品酒大会 后缀数组
  3. 使用Spring Security OAuth2进行简单的单点登录
  4. go实现主线程等待子线程都运行完再退出
  5. spring boot + spring data jpa
  6. Swing编程概述
  7. 《移动Web前端高效开发实战》笔记1——静态布局在移动端上的自适应
  8. java中的常用内存区域总结
  9. IOS制作纯色背景
  10. js当前日期