Solved

Exception Handling

Posted on 1998-11-18
10
1,118 Views
Last Modified: 2013-11-20
Here is a piece of my code:

      while (!end)
            {
                  CIntelHexaLine * line = new CIntelHexaLine;
                  try
                  {
                        line->Serialize(ar);
                  }
                  catch (CException * e)
                  {
                        end = TRUE;
                        e->Delete();
                  }
... more code

The Serialize function is:
void CIntelHexaLine::Serialize( CArchive& ar )
{
      if( ar.IsStoring( ) )
      {      
            ar.WriteString (strLine);
      }
      else      
      {                        
            ar.ReadString (strLine);
      }
}

ReadString will throw a CArchive:endoffile exception if it runs past the end of the file.
In the Output debug window i see the exception (lots of it because the while nevers end) but my catch block does not catch this.
why? How can i catch the exception?
0
Comment
Question by:jmartins71
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 3

Expert Comment

by:shaig
ID: 1324900
Try using Microsoft's macro's TRY  & CATCH_ALL
0
 

Author Comment

by:jmartins71
ID: 1324901
thanks shaig but that does not work.
i think that what you said is:
while (!end)
            {
                  CIntelHexaLine * line = new CIntelHexaLine;

                  TRY;
                        line->Serialize(ar);

                  CATCH_ALL(e)
                        end = TRUE;
                        e->Delete();
                  END_CATCH_ALL;
}
0
 
LVL 3

Expert Comment

by:shaig
ID: 1324902
No!!
you are not tring nothing like that.
do this:

TRY
{
   ....
}
CATCH_ALL...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jmartins71
ID: 1324903
It is not working!

From afx.h

#define TRY { AFX_EXCEPTION_LINK _afxExceptionLink; try {
#define CATCH_ALL(e) } catch (CException* e) \
      { { ASSERT(e->IsKindOf(RUNTIME_CLASS(CException))); \
            _afxExceptionLink.m_pException = e;
#define END_CATCH_ALL } } }


So the brackets are not necessary although i have put unnecessary ';'.

for the sake of it i have tried:
      TRY
                  {
                        line->Serialize(ar);
                  }
                  CATCH_ALL(e)
                        end = TRUE
                  END_CATCH_ALL

By the way i have discover that i don´t need to delete e.
0
 
LVL 7

Expert Comment

by:psdavis
ID: 1324904
Switching from try to TRY should be mute.

try
{
   while (!end)
   {
      CIntelHexaLine * line = new CIntelHexaLine;
      line->Serialize(ar);
   }
}
catch (CException * e)
{
   end = TRUE;
   e->Delete();
}

0
 

Author Comment

by:jmartins71
ID: 1324905
thanks psdavis but that is not working.

I have been told to use c++ try catch and not MFC macros
and i also think that it should be the same but nothing
is working.


More information :
I have tried in serialization function this:
void CIntelHexaLine::Serialize( CArchive& ar )
{
      if( ar.IsStoring( ) )
      {      
            ar.WriteString (strLine);
      }
      else      
      {                        
      //      ar.ReadString (strLine);
            throw (new CArchiveException(CArchiveException::endOfFile)) ;
      }
}
AND VOILÁ my exception was catched!!
Where ReadString is working on the exception?

and more:

I´m using VC++ 6.0 and what hits the output window is:
"...
First-chance exception in CSM Programmer.exe (MSVCRTD.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in CSM Programmer.exe (MSVCRTD.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in CSM Programmer.exe (MSVCRTD.DLL): 0xE06D7363: Microsoft C++ Exception.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile.
CArchive exception: endOfFile
."
0
 

Author Comment

by:jmartins71
ID: 1324906
I think i got the answer:

ReadString has to ways.

Bool ReadString(CString& rString );
and
LPTSTR ReadString( LPTSTR lpsz, UINT nMax );

only the last throw an exception!!!
And i am using the first!!!
Why does Microsoft does this?

I am now testing the return from ReadString and when false i throw one exception.




0
 
LVL 3

Accepted Solution

by:
shaig earned 0 total points
ID: 1324907
Are you sure that the exeption is comming from that location?
Add trace debugging befor and after the serializing and check if the exeptions accure between.
If not, you are looking in the wrong place.
0
 

Author Comment

by:jmartins71
ID: 1324908
Thanks shaig.
I think i got it.
By the way i am receiving the following in
my output window:
CArchive exception: endOfFile.
First-chance exception in CSM Programmer.exe (MSVCRTD.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in CSM Programmer.exe (MSVCRTD.DLL): 0xE06D7363: Microsoft C++ Exception.

Th first is normal because i make it but what about the other messages?
Is it normal?
I will give you the points and will be watching for some comments.

PS: Sorry about my delays but i am having lots of trouble with HotMail access.

0
 

Author Comment

by:jmartins71
ID: 1324909
Thanks shaig.
I think i got it.
By the way i am receiving the following in
my output window:
CArchive exception: endOfFile.
First-chance exception in CSM Programmer.exe (MSVCRTD.DLL): 0xE06D7363: Microsoft C++ Exception.
First-chance exception in CSM Programmer.exe (MSVCRTD.DLL): 0xE06D7363: Microsoft C++ Exception.

Th first is normal because i make it but what about the other messages?
Is it normal?
I will give you the points and will be watching for some comments.

PS: Sorry about my delays but i am having lots of trouble with HotMail access.

0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question