C++ 异常处理

C++ 异常处理
异常是程序在执行期间产生的问题。C++ 异常是指在程序运行时发生的特殊情况,比如尝试除以零的操作。

异常提供了一种转移程序控制权的方式。C++ 异常处理涉及到三个关键字:try、catch、throw。

throw: 当问题出现时,程序会抛出一个异常。这是通过使用 throw 关键字来完成的。
catch: 在您想要处理问题的地方,通过异常处理程序捕获异常。catch 关键字用于捕获异常。
try: try 块中的代码标识将被激活的特定异常。它后面通常跟着一个或多个 catch 块。

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std; 
 5 int main(int argc, char** argv) {
 6     int a[10];
 7     int i,j,t;
 8     cout<<"input 10 numbers:"<<endl;
 9     for(i=0;i<10;i++)
10     cin >>a[i];
11     cout <<endl;
12     for(j=0;j<9;j++)
13     for(i=0;i<9-j;i++)
14     if(a[i]>a[i+1])
15     {
16         t=a[i];
17         a[i]=a[i+1];
18         a[i+1]=t;
19     }
20     cout <<"the sorted numbers:" <<endl;
21     for(i=0;i<10;i++)
22     cout<<a[i]<<" ";
23     cout <<endl;
24     return 0;
25 }
原文地址:https://www.cnblogs.com/borter/p/9401265.html