?
Solved

Exception Handling

Posted on 1998-11-18
10
Medium Priority
?
1,121 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
Industry Leaders: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

771 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