[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1131
  • Last Modified:

Exception Handling

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
jmartins71
Asked:
jmartins71
  • 6
  • 3
1 Solution
 
shaigCommented:
Try using Microsoft's macro's TRY  & CATCH_ALL
0
 
jmartins71Author Commented:
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
 
shaigCommented:
No!!
you are not tring nothing like that.
do this:

TRY
{
   ....
}
CATCH_ALL...
0
Independent Software Vendors: 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!

 
jmartins71Author Commented:
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
 
psdavisCommented:
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
 
jmartins71Author Commented:
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
 
jmartins71Author Commented:
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
 
shaigCommented:
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
 
jmartins71Author Commented:
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
 
jmartins71Author Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now