Solved

Exception Handling

Posted on 1998-11-18
10
1,070 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
  • 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now