When will ofstream::open fail?
<p>I am trying out try, catch, throw statements in C++ for file handling, and I have written a dummy code to catch all errors. My question is in order to check if I have got these right, I need an error to occur. Now I can easily check <code>infile.fail()</code> by simply not creating a file of the required name in the directory. But how will I be able to check the same for <code>outfile.fail()</code> (<code>outfile</code> is <code>ofstream</code> where as <code>infile</code> is <code>ifstream</code>). In which case, will the value for <code>outfile.fail()</code> be true?</p>

<p>sample code [from comments on unapersson's answer, simplified to make issue clearer -zack]:</p>

<pre><code>#include &lt;fstream&gt;
using std::ofstream;

int main() 
{
    ofstream outfile;
    outfile.open("test.txt");
    if (outfile.fail()) 
        // do something...... 
    else 
        // do something else..... 
    return 0; 
}
</code></pre>
