1: //exception_classes1.cpp
3: #include <iostream>
4: #include <fstream>
5: #include <string>
6: #include <vector>
7: #include <stdexcept>
8: using namespace std;
10: #include "utilities.h"
11: using Scobey::Pause;
13: int main(int argc, char* argv[])
14: {
15: ifstream inFile;
16: try
17: {
18: //Test the command-line arguments
19: if (argc != 4)
20: throw invalid_argument("Error: Program requires 3 arguments.");
22: //Test whether an input file can be opened
23: inFile.open(argv[2]);
24: if (!inFile.is_open())
25: throw runtime_error("Error: " + string(argv[2]) + " could not be opened.");
26: //If file opened OK, process its contents ...
27: }
28: catch (invalid_argument e)
29: {
30: cout << e.what() << endl;
31: Pause(0, "Program is now terminating.");
32: return 0;
33: }
34: catch (runtime_error e)
35: {
36: //If something went wrong, close any opened files before terminating
37: inFile.close();
38: cout << e.what() << endl;
39: Pause(0, "Program is now terminating.");
40: return 0;
41: }
42: }