14.3 编写一个程序,显示一个整型静态数组 a(共 5 个元素)中指定下标的元素,并 用异常处理机制检测下标超界的情况。

来源:互联网 发布:关键词优化 编辑:程序博客网 时间:2024/05/16 09:38
#define _CRT_SECURE_NO_WARNINGS
/*
14.3 编写一个程序,显示一个整型静态数组 a(共 5 个元素)中指定下标的元素,并
用异常处理机制检测下标超界的情况。
};
*/
#include<iostream>
#include <math.h>
#include <iomanip>
#include <fstream>
//using namespace std;
#define MAX 5


void show(int a[],int n)
{
try
{
if (n > MAX)
throw n;
}
catch (int n)
{
std::cout << "下标越界---" << n << std::endl;
}


for (int i = 0; i < n; i++)
{
std::cout << a[i] << std::endl;
}
}




void main()
{
static int a[MAX] = { 3, 2, 4, 1, 5 };
show(a, 6);




system("pause");
}

0 0