Avatar of TheIronDuke
TheIronDuke

asked on 

Getting error information from catch(...)

I am managing exceptions using C++ MFC using the catch(...) option. Is there any way to get more information using this exception handler, more than just "and exception occurred"? Like Windows getLastError() or something?

Any help would be appreciated.
void myProgram
{
	CStdioFile		file;
	CString			szPath = _T("Myfile.txt");
	CString			szData,szError;
	bool			bFound = false;
	int				nErrors = 0;
	TCHAR			szCause[255];

	try
	{
		file.Open(szPath,  CFile::modeNoTruncate | CFile::modeReadWrite);
		file.SeekToEnd();
		while( file.ReadString(szData) && !bFound && !nErrors )
		{
			if( szData.Find(_T("MyName="),0) >= 0 )
			{
				someProcessProgram(szData);
				bFound = true;
			}
		}

		file.Close();

	}
	catch( CException* e )
	{
		e->GetErrorMessage(szCause,255);
		e->Delete();
		szError.Format(_T("An exception occurred while reading data.\nWindows reports: %s"),szCause);
		nErrors++;
		AfxMessageBox(szError);
	}
	catch( std::exception* e )
	{
		szError.Format(_T("An exception occurred while reading data.\nWindows reports: %s"),e->what());
		nErrors++;
		AfxMessageBox(szError);
	}
	catch(...)
	{
		szError = _T("An exception occurred while reading data.");
		nErrors++;
		AfxMessageBox(szError);
	}

}

Open in new window

C++System Programming

Avatar of undefined
Last Comment
Frank Helk
Avatar of Manuel Flores
Manuel Flores
Flag of Spain image

You could return or show the CFileException::GetErrorMessage content.
Avatar of TheIronDuke
TheIronDuke

ASKER

I was more interested in what I can get from the "catch(...)" exception.

CException should give me CFileException exceptions, correct? At least that's the way I understand it.
Exactly because is an abstract class implemented by all CXXXXExceptions subclasses.

I think indeed that the way you have programmed it, i.e. using the superclass, will catch all errors.

Anyway;  you can't actually see the exception variable that is thrown when using catch(...)
Some tips from other forums;

"....This is how you can reverse-engineer the exception type from within catch(...) should you need to (may be useful when catching unknown from a third party library) with GCC:
    catch(...)
    {
        std::exception_ptr p = std::current_exception();
        std::clog <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
    }
SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of pepr
pepr

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Frank Helk
Frank Helk
Flag of Germany image

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Zoppo (https:#a41748384)
-- pepr (https:#a41748372)
-- sarabande (https:#a41747588)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo