Solved

Try Catch question in C 2008

Posted on 2011-09-21
12
395 Views
Last Modified: 2012-05-12
Dear experts!

I’m new to MS C 2008 and I need a little help. Look my code

long CCPlayCtrl::GetCurrPlayFilePosMS(long lCard)
{
   hDecoder=hDec[lCard];
   double dRes = 0; long lRes=-99

   try
   {
    lRes = (long)StradisDecoder_Decoder_GetFrame(hDecoder);
   dRes=lRes * mFramePerMsSeconds[lCard];
   
   lRes=(long)dRes;
   }
   catch (COleException ex)
    {
        lRes=-99; //there is error - result should be ignored! Hope next time to be correct!
      }

   return lRes;
}

StradisDecoder_Decoder_GetFrame is in external DLL and is provide from manufucter of STradis hardware card. My question is for this code

catch (COleException ex)

this is OK? It should be ColeException or other? I do not interested to get real error in ex values, if there is any error result should be ignored.  I’m not sure, but I think it is possible generated error to not fit in ColeException and this to produce second error.
0
Comment
Question by:dvplayltd
  • 7
  • 3
  • 2
12 Comments
 
LVL 1

Expert Comment

by:RG59
ID: 36578490
Add a second catch block that just catches Exception and handle it there.

catch (COleException cex)
{
// handle COleException
}
catch (Exception ex)
{
// handle Exception
}
0
 

Author Comment

by:dvplayltd
ID: 36578577
Hi RG59

Exception is most common error, right ? I mean - EVERY type of error will be catch by Exception?
0
 

Author Comment

by:dvplayltd
ID: 36578591
      catch (Exception ex)

return error: Error      8      error C2061: syntax error : identifier 'Exception'      d:\AAdInserter\StardisOCX\CPlay\CPlayCtl.cpp      959      CPlay

Remember, we are in MS C 2008. Is it possible you to have mistake with C# ???
0
 

Author Comment

by:dvplayltd
ID: 36578756
Well, i find that             catch (...) may be will work. Again, I need just to catch error, not need to find it what it is.  But I DO need to not get message which wait for user to click OK.

Back to catch (...)

But as I understand this catch all C++ errors, not C. Well, i think that the extalan DLL is written on C, then what type is his error returned ??? I mean this function.

   lRes = (long)StradisDecoder_Decoder_GetFrame(hDecoder);

My project is On MS C 2008 with cpp files, the extarnal DLL have this row in his Header file.

extern "C" {
#endif

0
 
LVL 1

Expert Comment

by:RG59
ID: 36578821
Hi wrote the code snippet on my iPad this morning, typing straight in to the browser so it may not be typo free but the principle is the same. You can have as many catch blocks as you want. The first catch block should be the most specific (i.e. have the mosts specific error in the catch clause (COleException in your case).

When a error happens .Net will look at the first catch block to see if the error matches the catch clause. If it does it will execute the code in the relevant block. If not it will move on to the next catch clause in the list.

Having Exception in your final catch clause will catch any errors not previously caught. Therefore, the code will never break and, unless you code it in the final catch block, now message will be shown.

Clearly, you risk an error condition causing problems later in your code, but it's up to you to code around that.

Clearly, this is all happening inside you're c# code. Which should then survive any errors that get thrown inside external unmanaged code that you've called.

I hope this helps.
0
 

Author Comment

by:dvplayltd
ID: 36579030
To RG59

Yfff. Do you read at all my question?

I’m new to MS C 2008 and I need a little help. Look my code


I'M NOT IN C# ! I'm in MS C 2008 unmanaged project .
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Expert Comment

by:RG59
ID: 36579104
if you're gonna be rude mate I suggest you fix it yourself. I was trying to be helpful.
0
 
LVL 7

Accepted Solution

by:
tampnic earned 500 total points
ID: 36579427
Straight C code does not implement exception handling. If your external library is written in C it will not throw C++ exceptions - so the catch blocks will never be called if an error occurs within the library. Only errors within your own C++ caling function will be handled e.g. if lCard is out of bounds for your mFramePerMsSeconds array.

Normally with C code you validate a return value from a function to check if an error occurred, or use a platform specific function such as GetLastError() on Windows to see if a system error has occurred.

http://www-users.math.umd.edu/~asnowden/c-except.html shows a method of simulating C++ exception handling in C code if you are interested.

Cheers,
  Chris.
0
 

Author Comment

by:dvplayltd
ID: 36579480
Hi Chris!

Thank you for clear and profesional answer!

Well, most function of the DLL return back result, but excatly the properties does not. But if fill a value and I'll check this value for zero or negative value. I'm almost sure it will work!

       try
       {
                      StradisDecoderSettings_GetFrameCount(hWng1); // this function produce error
       }
       catch (...)
       {
                         lRes=-99; goto ExitWithError;      // I succed to test it - as you say - the error is not catch here becaouse it is real C, not C++
       }

       long l=(long)StradisDecoderSettings_Get_m_dwFrameCount(hWng1);  // Here I should check for value >0

I show you up code to see that I ask a real question :-)

Well, I'm ready to give you points and to my BIG THANKS, but please tell me - if I do  method of simulating C++ exception handling in C code - will this work with external DLL like is my case ?
0
 
LVL 7

Expert Comment

by:tampnic
ID: 36579570
The DLL has to be written to use the simulated exception behaviour. If you have access to the source code for your DLL you could add the exception handling yourself. If you don't have the source code for the DLL you could not make it throw exceptions.


You could try something like

 
try 
{
  long l=(long)StradisDecoderSettings_Get_m_dwFrameCount(hWng1);       
  if (l <= 0 ) throw _T("Can't Get frame Count!"); // throw a text string exception
}
catch (LPCTSTR str)
{
  // the exception text can be accessed using the str variable in this catch block
  MessageBox(NULL,str,_T("Error"),MB_OK);
  lRes=-99; goto ExitWithError;  
}

Open in new window


if you want to use exception handling in your own code.

Cheers,
  Chris
0
 

Author Comment

by:dvplayltd
ID: 36579598
Well, i guest you code will working. But why to generate exception, I made it directly like this:

       try
       {
        StradisDecoderSettings_GetFrameCount(hWng1);
       }
       catch (...)
       {
       lRes=-99; goto ExitWithError;      
       }

       long l=0;
       l=       (long)StradisDecoderSettings_Get_m_dwFrameCount(hWng1);
     
       if (l<=0)
       {
        lRes=-99; goto ExitWithError;      
       }
0
 

Author Closing Comment

by:dvplayltd
ID: 36579602
10x. I use idea and it is already implement in project, and it is working too. I make test. GREAT THANKS!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

743 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

14 Experts available now in Live!

Get 1:1 Help Now