Solved

Exception Handling

Posted on 1998-11-18
10
1,082 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven archtype selection in eclipse 1 53
Hibernate methods 2 68
Doc'in system (example?) BA 7 89
Is there a simple front-end menu system. 9 71
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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. …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

895 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