---
# System prepended metadata

title: 得知錯誤

---

# 得知錯誤
```
#include <iostream>
#include <fstream>
#include <cstdio>
#include <windows.h>
#include <stdexcept>
using namespace std;

class file: public exception {};
class over: public exception {};
class num: public exception {};

int data()
{
    fstream fin;
    fin.open("func.txt");
    if(!fin)
        throw file();
    int n, i = -1;
    fin >> n;
    int *a = (int*) malloc(n * sizeof(int));
    double b;
    while(!fin.eof())
        {
            i++;
            if(i + 1 > n)
                throw num();
            fin >> b;
            if( b >= 2147483647 || b <= -21474836477)
                throw over();
            a[i] = b;
            //    cout << a[i] << endl;
        }
    if(i + 1 < n)
        throw num();
    return 0;
}

int main()
{
    try
        {
            data();
        }
    catch(file)
        {
            cout << "檔案不存在" << endl;
        }
    catch(over)
        {
            cout << "溢位" << endl;
        }
    catch(num)
        {
            cout << "與範圍不符" << endl;
        }
    return 0;
}

```