CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    [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

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Try, Catch Problem

    By default, exceptions are not turned on for a stream object.

    Code:
    inputFile.exceptions(ios::failbit | ios::badbit); 
       
    try

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb 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

  4. #4
    Join Date
    Jun 2007
    Location
    .NET 3.5 Beta SP1, Visual Basic 2008 Express
    Posts
    225

    Re: Try, Catch Problem

    thanks.
    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
  •  





Click Here to Expand Forum to Full Width

Featured