Obtaining error messages in C++

This is a discussion on Obtaining error messages in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi! How does one get access to the standard error messages in C++? In C you can get the error ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    65

    Obtaining error messages in C++

    Hi! How does one get access to the standard error messages in C++?

    In C you can get the error message from strerror(), provided you have the error code. How can something similar be achieved using the iostreams library in C++? I thought that the exception messages could be useful but doesn't seem like it. "Underflow error reading the file" isn't too helpful :-/ And looking for "c++ error message" on the internet gets only pages on compiler error messages

    I'm looking for the equivalent of:
    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    
    int main(void)
    {
        // pretend a directory is a file
        FILE* f = fopen("/", "r");
        getc(f);
    
        // print the resulting error message
        char* error_message = strerror(errno);
        // output: Oopsie: Is a directory
        printf("Oopsie: %s\n", error_message);
    
        return 0;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Code:
    cout << "Output.";   // stdout
    cerr << "Error.";   // stderr unbuffered
    clog << "Log.";    // stderr buffered
    I may be wrong about what file descriptor clog uses, but whatever.

    Besides, the more common way to output to stderr in C would be:
    Code:
    fprintf(stderr,"Error");
    EDIT:: SORRY MISUNDERSTOOD THE PROBLEM. CONTINUE TO MY OTHER REPLIES.
    Last edited by SlyMaelstrom; 03-03-2006 at 03:14 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    I'm not asking about how to print to cerr or clog. I'm asking what to print there, that is, from where I can get the error message---preferrably localized. That is, the string that strerror of C returns.

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I think its the same
    Just include "cerrno"

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Hows this?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cerrno>
    
    using namespace std;
    
    int main()
    {
        ifstream inFile("", ios_base::in);
        
        string error = strerror(errno);
        cerr << error;
        cin.get();
        return 0;
    }
    Last edited by SlyMaelstrom; 03-03-2006 at 03:07 PM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    I believe that C++ dropped the errono setup, and instead uses exceptions for passing error information. Mind you if you use the C libraries, they will still use it.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Or as Xipher said, with exceptions:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
       int errorVal;
       try {
         ifstream inFile("", ios_base::in);
         throw errorVal;
       }
       catch(int error) {
          if (error == 8)
             cerr << "No such file or directory.";
          else
             cerr << "Unknown Error.";
       }
       return 0;
    }
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Thanks SlyMaelstrom and MadCow, strerror does seem to work even though I had doubts about it.

    It doesn't seem to co-operate with exceptions though. Is there a way to get a meaninful error message when you are using exceptions?

    That is, using something like
    Code:
        try 
        {
    	ifstream in;
    	in.exceptions(ifstream::badbit | ifstream::failbit);
    	in.open(filename);
    
    	string line;
    	while (getline(in, line))
    	{
    	    process(line);
    	}
        }
        catch (ios_base::failure &ex)
        {
            // how to access the error message here?
    	cout << "Problem: " << strerror(errno) << endl;
    	cout << "Problem: " << ex.what() << endl;
        }
    ex.what seems to give "basic_ios::clear" when the file doesn't exist and "basic_filebuf::underflow error reading the file" when it's a directory. Calling strerror in the catch block gives the real error message when the file doesn't exist (yay!), but "success" when it's a directory.

    Oh well, I guess you can't have everything, C-style error handling isn't that bad
    Last edited by joni; 03-03-2006 at 03:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining useful information from Crash Report
    By @nthony in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2009, 09:37 PM
  2. Replies: 21
    Last Post: 09-17-2008, 07:37 AM
  3. Obtaining real types from variants
    By therealwill in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2006, 06:15 AM
  4. Obtaining a txt file through the web
    By robid1 in forum C Programming
    Replies: 7
    Last Post: 10-24-2003, 05:34 AM
  5. Obtaining data through RS-232 port...
    By johny780 in forum C Programming
    Replies: 9
    Last Post: 10-08-2003, 10:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21