-
October 2nd, 2008, 10:05 AM
#1
[RESOLVED] Try, Catch Problem
I have a try catch block that should, when it doesn't work, display a message saying the file couldn't be found. Unfortunately, all it does is just go back to asking for a file. Here's the code, why won't it do the cout in the catch block?
Code:
bool readArray (double x [], int &n)
{
int i;
char filename[51];
ifstream inputFile;
bool success;
cout << "Enter filename (and path, if needed):" << endl;
cin.getline(filename, 51);
cout << endl << endl;
try
{
inputFile.open(filename);
}
catch(...)
{
cout << "Error opening file." << endl << endl;
success = false;
}
if (success != false)
{
//read file stuff
}
return success;
}
Microsoft Visual Basic 2008 Express Edition
.NET Framwork 3.5 Beta SP1
-
October 2nd, 2008, 10:18 AM
#2
Re: Try, Catch Problem
By default, exceptions are not turned on for a stream object.
Code:
inputFile.exceptions(ios::failbit | ios::badbit);
try
-
October 2nd, 2008, 10:19 AM
#3
Re: Try, Catch Problem
Take a look at this:
http://www.cplusplus.com/reference/i...xceptions.html
The filestream won't throw exceptions unless you configure it to do so.
Or try this if you choose to just use a member function to test the stream state rather than configure the exception handling.
http://www.cplusplus.com/reference/i...ream/open.html
-
October 2nd, 2008, 10:43 AM
#4
Re: Try, Catch Problem
Microsoft Visual Basic 2008 Express Edition
.NET Framwork 3.5 Beta SP1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|