Link to home
Start Free TrialLog in
Avatar of dvplayltd
dvplayltdFlag for Bulgaria

asked on

Try Catch question in C 2008

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.
Avatar of RG59
RG59
Flag of United Kingdom of Great Britain and Northern Ireland image

Add a second catch block that just catches Exception and handle it there.

catch (COleException cex)
{
// handle COleException
}
catch (Exception ex)
{
// handle Exception
}
Avatar of dvplayltd

ASKER

Hi RG59

Exception is most common error, right ? I mean - EVERY type of error will be catch by Exception?
      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# ???
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

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.
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 .
if you're gonna be rude mate I suggest you fix it yourself. I was trying to be helpful.
ASKER CERTIFIED SOLUTION
Avatar of tampnic
tampnic
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ?
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
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;      
       }
10x. I use idea and it is already implement in project, and it is working too. I make test. GREAT THANKS!