Help me with Exceptions and improvement of class

Here is my class. Where should I use exceptions or try catch. I am really weak at exceptions
Any design suggestion will make me happy too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm> //copy
#include <iterator> //back_inserter

/******************************************************************
 * File Handler class
 * By: //////////////////
 * Version 1.0 (c++11)
 * ***************************************************************/
class FileHandler
{
private:
    std::ofstream m_output; //output
    std::ifstream m_input;  //input
    unsigned m_iNext;   //Keep Track of returned lines
                        //container for input strings
    std::vector<std::string> m_vInputContainer;
    std::vector<std::string> m_vOutputContainer;
public:
    FileHandler():m_iNext(0){}
    ~FileHandler(){
        if(m_output.is_open()) m_output.close();
        if(m_input.is_open()) m_input.close();
    }
    
    /***************************************************************
     * INPUT File Section 
     * Functions list:
     * 1- Make a connection to file(open) and load content to container
     * 2- Empty container
     * 3- return next line in container as string
     * 4- Check if cursof reached eof
     ***************************************************************/
    
    //1- Make a connection to file(open) and load content to container
    bool Input(const std::string file_name)
    {
        //1-1- OpenFile
        m_input.open(file_name, std::ios_base::in);
        //Fast Error checking
        bool fileConnectionState = m_input.good();
        //1-2-IF connection is OK get file's contect line by line to container
        if(fileConnectionState)
        {
            std::string line;
            while(std::getline(m_input, line))
            {
                m_vInputContainer.push_back(line);
            }
            //After loading file content close the file
            m_input.close();
        }
        //1-3- return file state. True means file is Opend and Content loaded at
        //container
        return fileConnectionState;
    }
    
    //2- empty container
    void clearContainer()
    {
        m_vInputContainer.erase(m_vInputContainer.begin(), m_vInputContainer.end());
        //set line tracker for next use
        m_iNext = 0;
    }
    
    //3- return next line in container as string
    std::string Next()
    {
        return m_vInputContainer.at(m_iNext++);
    }
    
    //4- Check if cursof reached eof
    bool reachedEnd()
    {
        // Return true if reached eof
        return m_iNext >= m_vInputContainer.size();
    }
    
    /*****************************************************
     * OutPut File Section
     * Fuinction list:
     * 1- Opent out put file :@priavate
     * 2- Add to output container
     * 3- Copy input container to output container
     * 4- write  output container to file
     *****************************************************/
     //1- Opent output file: @private
private:
     bool output(const std::string filename)
     {
         m_output.open(filename, std::ios_base::out);
         return m_output.is_open();
     }
     //In case appending to output
     bool appendOutput(const std::string filename)
     {
         m_output.open(filename, std::ios_base::out|std::ios_base::app);
         return m_output.is_open();
     }

public:
     //2-Add to output container
     void addToOutputContainer(std::string str)
     {
         m_vOutputContainer.push_back(str);
     }
     
     //3- Copy input container to output container
     void copyInputToOutput()
     {
         std::copy(m_vInputContainer.begin(), m_vInputContainer.end(), std::back_inserter(m_vOutputContainer));
     }
     
     //4- write output container to file
     void writeToOutput(std::string filename)
     {
         output(filename);
         for(auto& x: m_vOutputContainer)
         {
             m_output << x << std::endl;
         }
         m_output.close();
     }
     
     //In case appending
     void appendToOutput(std::string filename)
     {
         appendOutput(filename);
         for(auto& x: m_vOutputContainer)
         {
             m_output << x << std::endl;
         }
         m_output.close();
     }
    
};
Last edited on
Any design suggestion will make me happy too.

What is the motivation for this class?
Where should I use exceptions

You are already using them. FileHandler::input, FileHandler::addToOutputContainer, FileHandler::copyInputToOutput can throw std::bad_alloc, and FileHandler::next can throw std::out_of_range.

Since your constructor doesn't do anything meaningful and there is no invariant I can see, there is no reason to throw your own. Well, since you're calling fstream::close directly (why?), you could pretend your invariant is that both member streams are closed (why are they members?) and throw if close fails.

In general, see http://en.cppreference.com/w/cpp/language/exceptions#Usage for exception usage.

or try catch

not in this class
Last edited on
What is the motivation for this class?

Just a fun class for practice And personal usage

You are already using them. FileHandler::input, FileHandler::addToOutputContainer, FileHandler::copyInputToOutput can throw std::bad_alloc, and FileHandler::next can throw std::out_of_range.

Wow thats really helped. I thought I should do it for every single line of code my self. Thanks stl and thanks cubbi
You are confused about this because you are the person designing the class and you are the person using it. Exceptions doesn't make sense here. Use error codes. Exceptions are more useful when your are using someone else's code and you want at least well defined exit if his/her code encounters something nasty (exceptions).

If you are using that for the sake of learning, think how you might help convey the problem if anything happens, to your code user. And then design your exceptions around that concept.
When designing a class, I find it helpful to think that the methods should not "do the work." Instead they should "make doing the work easy." If you think this way, you'll end up with a much more flexible class. Consider for example:

What if I want to write some stuff to a file, then write your class to the file, then write some more stuff myself? Right now I'd have to write my data, close the file, tell your class to append, then open the file again in append mode, then continue writing. Yuck.

What if I have already read the file into a string and want to use your class to process the contents of the string? I have to write the string to a file and then use your class.

To avoid these problems, the class should be a StreamHandler instead of a FileHandler. Anywhere you have a file stream, it should just be a stream instead. Don't open/close the streams in the class - let the caller do that.
dhayden:......
ajith mk:......


I am coding just for fun for myself. Never been in actual programming projects. I need more study
Topic archived. No new replies allowed.