CFileException, discover reasons RESOLVED
-
Windows 7, 64 bit, Visual Studio 2008, MFC, C++ After a long series of searches looking for a nice method of reading from a text file into CString I arrived at the following:
CStdioFile definition_file; try { definition_file.Open( m_definition_file_name, CFile::modeRead ); } catch( CFileException e ) { e.m_cause; }
This is from the VS help utility:catch( CFileException* e ) { if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n") e->Delete(); }
This leads to the conceptif( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n"); elseif( e->m_cause == CFileException::generic ) printf( "ERROR: An unspecified error occurred.\n"); elseif etc etc
Is there a better way of capturing the text of the error rather than an if/else/else/else sequence? Note: I am open to any type of suggestion. The goal is to read a CSV text file and parse it into a std::map.Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Windows 7, 64 bit, Visual Studio 2008, MFC, C++ After a long series of searches looking for a nice method of reading from a text file into CString I arrived at the following:
CStdioFile definition_file; try { definition_file.Open( m_definition_file_name, CFile::modeRead ); } catch( CFileException e ) { e.m_cause; }
This is from the VS help utility:catch( CFileException* e ) { if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n") e->Delete(); }
This leads to the conceptif( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n"); elseif( e->m_cause == CFileException::generic ) printf( "ERROR: An unspecified error occurred.\n"); elseif etc etc
Is there a better way of capturing the text of the error rather than an if/else/else/else sequence? Note: I am open to any type of suggestion. The goal is to read a CSV text file and parse it into a std::map.Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Use the
GetErrorMessage
[^] member function.Use the best guess
Cool. Thank you.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Use the
GetErrorMessage
[^] member function.Use the best guess
Oops, I have a follow up on this. Declarations:
CFileException get_error_message;
const unsigned int ERROR_MAX = 64;
char char_error_text[ ERROR_MAX ];
LPTSTR ptr_error_text = &char_error_text[0];
CString final_error_message = "";Code:
m_definition_file_name += "x";
try
{
m_definition_file.Open( m_definition_file_name, CFile::modeRead );
get_error_message.GetErrorMessage( ptr_error_text, ERROR_MAX, NULL );
final_error_message = char_error_text;
error_number = GetLastError();}
catch( CFileException e )
{ code }
catch( ... )
{ code }
{The line m_definition_file_name += "x" forces the file name to be incorrect. Char array char_error_text winds up with "No error occurred." while error_number gets 0 (zero) When the line error_number = GetLastErrror is moved directly beneath the open command, it get the value 2 (two). But that consumes the last error and get_error_message does not get anything. Nothing is thrown and neither catch is activated. What do I need to change to use this CFileException?
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Windows 7, 64 bit, Visual Studio 2008, MFC, C++ After a long series of searches looking for a nice method of reading from a text file into CString I arrived at the following:
CStdioFile definition_file; try { definition_file.Open( m_definition_file_name, CFile::modeRead ); } catch( CFileException e ) { e.m_cause; }
This is from the VS help utility:catch( CFileException* e ) { if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n") e->Delete(); }
This leads to the conceptif( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n"); elseif( e->m_cause == CFileException::generic ) printf( "ERROR: An unspecified error occurred.\n"); elseif etc etc
Is there a better way of capturing the text of the error rather than an if/else/else/else sequence? Note: I am open to any type of suggestion. The goal is to read a CSV text file and parse it into a std::map.Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
I have also tried this. Declarations:
const unsigned int ERROR_MAX = 64;
char char_error_text[ ERROR_MAX ];
LPTSTR ptr_error_text = &char_error_text[0];
CString final_error_message = "";With this code
m_definition_file.Open( m_definition_file_name, CFile::modeRead );
error_number = GetLastError();
format_message_return = FormatMessage( 0, // dwFlags
NULL, // lpSousrce
error_number, // dwMessageID
NULL, // dwLanguageId
ptr_error_text, // lpBuffer
ERROR_MAX, // nSize
NULL ); // Arguments
final_error_message = char_error_text;The error number is 2 (two). char_error_text ends up with nothing. I am uncertain of the arguments for FormatMessage. The file name is forced to be incorrect and an error is expected.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Oops, I have a follow up on this. Declarations:
CFileException get_error_message;
const unsigned int ERROR_MAX = 64;
char char_error_text[ ERROR_MAX ];
LPTSTR ptr_error_text = &char_error_text[0];
CString final_error_message = "";Code:
m_definition_file_name += "x";
try
{
m_definition_file.Open( m_definition_file_name, CFile::modeRead );
get_error_message.GetErrorMessage( ptr_error_text, ERROR_MAX, NULL );
final_error_message = char_error_text;
error_number = GetLastError();}
catch( CFileException e )
{ code }
catch( ... )
{ code }
{The line m_definition_file_name += "x" forces the file name to be incorrect. Char array char_error_text winds up with "No error occurred." while error_number gets 0 (zero) When the line error_number = GetLastErrror is moved directly beneath the open command, it get the value 2 (two). But that consumes the last error and get_error_message does not get anything. Nothing is thrown and neither catch is activated. What do I need to change to use this CFileException?
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
I don't understand what you are doing here; it should be:
try
{
// code to open the CFile
}
catch (CFileException ce)
{
TCHAR message[132];
if (ce.GetErrorMessage(message, 132, NULL)
{
// display the contents of the message buffer
}
}Use the best guess
-
I don't understand what you are doing here; it should be:
try
{
// code to open the CFile
}
catch (CFileException ce)
{
TCHAR message[132];
if (ce.GetErrorMessage(message, 132, NULL)
{
// display the contents of the message buffer
}
}Use the best guess
Hello, I don't see what you have different. Second, when the file does not exist, this Open method does not throw an exception, so none of the try/catch matters. I had a catch(...) at the bottom and it was not entered.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Hello, I don't see what you have different. Second, when the file does not exist, this Open method does not throw an exception, so none of the try/catch matters. I had a catch(...) at the bottom and it was not entered.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
If the open function does not throw an error then you need to check the return value and call
GetLastError()
to find out why, as described here[^]. Alternatively you can throw a new exception like:try
{
if (!m_definition_file.Open(m_definition_file_name, CFile::modeRead))
{
DWORD error_number = GetLastError();
throw new CFileException::OsErrorToException(error_number);
}
}
catch( CFileException e )
{
// handle the exception
}Note that when calling
GetLastError()
you must call it immediately after the failing API function call, otherwise its return may not be valid.Use the best guess
-
Windows 7, 64 bit, Visual Studio 2008, MFC, C++ After a long series of searches looking for a nice method of reading from a text file into CString I arrived at the following:
CStdioFile definition_file; try { definition_file.Open( m_definition_file_name, CFile::modeRead ); } catch( CFileException e ) { e.m_cause; }
This is from the VS help utility:catch( CFileException* e ) { if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n") e->Delete(); }
This leads to the conceptif( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n"); elseif( e->m_cause == CFileException::generic ) printf( "ERROR: An unspecified error occurred.\n"); elseif etc etc
Is there a better way of capturing the text of the error rather than an if/else/else/else sequence? Note: I am open to any type of suggestion. The goal is to read a CSV text file and parse it into a std::map.Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
I have yet to get a text based error code for opening a file. Here is the simplified code:
m_definition_file_name = Select_Definition_File.GetPathName();
m_definition_file_name += "x"; // make the file name bad
m_definition_file.Open( m_definition_file_name, CFile::modeRead );// error_number = GetLastError(); // restore to verify error code is 2
get_error_message.GetErrorMessage( ptr_error_text, ERROR_MAX, NULL );
final_error_message = char_error_text; // says no errorThe Open method does not throw an exception. When GetLasError() is used, the error code is 2 (two). The GetErrorMesssage yields No error returned, which is incorrect. The file could not be opened because it does not exist. GetErrorMessage(...) is has three arguments about formatting the error message, but the error message is not an argument. I presume it gets the error message like GetLastError() does, but this appears incorrect. How do I get a text string describing this error?
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
I have yet to get a text based error code for opening a file. Here is the simplified code:
m_definition_file_name = Select_Definition_File.GetPathName();
m_definition_file_name += "x"; // make the file name bad
m_definition_file.Open( m_definition_file_name, CFile::modeRead );// error_number = GetLastError(); // restore to verify error code is 2
get_error_message.GetErrorMessage( ptr_error_text, ERROR_MAX, NULL );
final_error_message = char_error_text; // says no errorThe Open method does not throw an exception. When GetLasError() is used, the error code is 2 (two). The GetErrorMesssage yields No error returned, which is incorrect. The file could not be opened because it does not exist. GetErrorMessage(...) is has three arguments about formatting the error message, but the error message is not an argument. I presume it gets the error message like GetLastError() does, but this appears incorrect. How do I get a text string describing this error?
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
I gave you a suggested solution at http://www.codeproject.com/Messages/4583835/Re-CFileException-discover-reasons.aspx[^], which should throw an exception whose value would be
CFileException::fileNotFound
(error code 2). Did you try it, and if so what result did you get? In response to your comment aboutFormatMessage
, you need to tell the function where to look for the mappings of error codes to messages thus:format_message_return = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // dwFlags
NULL, // lpSousrce
error_number, // dwMessageID
NULL, // dwLanguageId
ptr_error_text, // lpBuffer
ERROR_MAX, // nSize
NULL ); // ArgumentsThis is explained in full detail in the documentation[^].
Use the best guess
-
I gave you a suggested solution at http://www.codeproject.com/Messages/4583835/Re-CFileException-discover-reasons.aspx[^], which should throw an exception whose value would be
CFileException::fileNotFound
(error code 2). Did you try it, and if so what result did you get? In response to your comment aboutFormatMessage
, you need to tell the function where to look for the mappings of error codes to messages thus:format_message_return = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // dwFlags
NULL, // lpSousrce
error_number, // dwMessageID
NULL, // dwLanguageId
ptr_error_text, // lpBuffer
ERROR_MAX, // nSize
NULL ); // ArgumentsThis is explained in full detail in the documentation[^].
Use the best guess
My apologies for no replying sooner. I followed your advice and now have that FormatMessage method working. Thank you.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
My apologies for no replying sooner. I followed your advice and now have that FormatMessage method working. Thank you.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/