Link to home
Start Free TrialLog in
Avatar of sneeuw
sneeuwFlag for Belgium

asked on

MediaPlayer exceptions isn't caught ! (Borland Builder)

Hi,

I Program under Borland C++ Builder 3.
(Beginner but eager to learn)

When I load a file in the MediaPlayer (e.g. an AVI file) and next click a button that does : MediaPlayer->Close(), an exception occurs !!!!
!! and it can't be caught by try { } catch (...) {  }  ??!

I tried following code :

try {
       MediaPlayer->FileName = " " ;
       MediaPlayer->Open() ;
       // MediaPlayer->Close() ;  
       // Causes 'MCI device not loaded'  Exception !
       }
 catch (...) {
       Caption = IntToStr(MediaPlayer->Error) ;
       }

But there was already another file loaded and Opened !
Yet, when the Exception occurs, it should just be caught by the 'try' 'catch' routine, is it not !!?

Now the Exception window still pops up (Also outside Builder !!)

Any ideas on this !??
Why in is the &%$@@&*$$ exception not caught by the try { ... routine ??

Hope someone can help.
Avatar of nietod
nietod

Someone else can probably help you more with this.  But until they come along the problem, is that the exception you encountered is one of Windows's structured exceptions.  These are similar to C++'s exceptions, but they are not the same.  They must be caught using a different mechanism.  (they are language indipendant.)
I've never fooled wtih the windows structured exceptions, but the C++ code you would use would be like:
__try {
      MediaPlayer->FileName = " " ;
          MediaPlayer->Open() ;
          // MediaPlayer->Close() ;  
          // Causes 'MCI device not loaded'  Exception !    
    }
__except( GetExceptionCode() == ??????) {
      Caption = IntToStr(MediaPlayer->Error) ;
    }

The ???? is the exception code value you need to test for.  You might want to look up "structured exception handling" in the docs and see what you can find.
In Visual C++, catch (...) also catches Windows structured exceptions. There is also a way to convert WSEs to C++ exceptions, but I never fooled with it...
I think what you do is to catch the WSE and then throw a C++ exception.  Like

try {
    __try {
           MediaPlayer->FileName = " " ;
               MediaPlayer->Open() ;
               // MediaPlayer->Close() ;    
               // Causes 'MCI device not loaded'  Exception !      
            }
     __except( GetExceptionCode() == ??????)
         {
            throw SomeCPlusPlusException;
         }
    }
    catch (...)
    {
          Caption = IntToStr(MediaPlayer->Error) ;
    }  

I'm surprised no one has answered this one yet.
Avatar of sneeuw

ASKER

Hi guys,

Thanks for the input so far.
I'll try some of the code you posted.

I hope to be able to tell you more tomorrow.
In the mean time, please keep on it ...
So far nobody really seems to know for sure.

Anyway ...
Again, ... thanks for the input !

Peter
Actually, I think you cen specify a function that gets called whenever a WSE is raised. You can make this function throw your own exceptions, and then ctch them using
    catch (MyException x) {/*...*/}

In any case, I tried, and the following line does catch WSEs:
    catch (...) {/*...*/}
Avatar of sneeuw

ASKER

Hi,

I looked up the syntax for catching WSE under Borland C++ Builder.  So I was able to write the following code (which compiles) :

try {
    try {
    MediaPlayer->FileName = " " ;
    MediaPlayer->Open() ;
    // Error is : No MCI device open
    }
    __except(EXCEPTION_EXECUTE_HANDLER) {
    Caption = "Windows Structured Exception caught" ;
    // Throw own exception
    // throw ("My own Exception") ; // With or without : has no effect
    }
}
catch (...) {
//
// Caption = "Exception caught" ;
}

The problem however still exists !!!
The exception IS caught (since the __except routine is executed (seen in Caption)).
However ... The popup Window with the error message "No MCI device open also Still pops up !!!
What next ??????

Peter
Avatar of sneeuw

ASKER

If I would write a routine to catch all the application's exceptions (an OnException routine), do you think I would be able to catch this nasty Exception ?????????


When does the pop-up appear?  before the exception is thrown?
Avatar of sneeuw

ASKER

I think AFTER the exception is thrown !
But how exactly can I make sure ?
Put a debugger breakpoint in the exception catching code.  Does the  deubgger stop in the catching code before or afte the message appears.  (does the debugger stop in the catching code at all--if not you aren't catching the exception.)

If the message appears after the exception is caught, you mus be retrhowing the exception somehow.  If the message appears before the  exception is caught, you either are catching too late or, more likely, the message is displayed before the exception is throw which means you are out of luck.
Avatar of sneeuw

ASKER

Hi guys,

THE PROBLEM IS SOLVED !

I found it to be a 'self-created' problem.
In fact the Exception occurred each time in another routine !
I had an OnNotify routine that 'of course' also was executed each time I 'Closed' the MediaPlayer.

I began to suspect something fishy when I now sometimes had two Exceptions in a row or suddenly No error at all.  Depending on the State of some other settings, the OnNotify handler would evoke no, one or more exceptions I hadn't thought of.

It was also your input 'Question : Error BEFORE or AFTER Exception' that got me searching somewhere else.

So, ...
- Sorry I put you up to the work you guys did.
- I learned something new (WSE) I had never heard of (I'm new in programming).
- You BOTH deserve a piece of the pie.  I suggest I give you both (yonat and nietod) 125 points !??
Can you live with that ?
One question though !  How do I assign points to you ?? (I've never done this !).

Thanks again,
Peter
Unfortunately, you can't split points.  only one expert can answer (and none has so far) a question and they will get the points when you grade it (unless you reject the answer.)  You must decide which expert (if any) is most deserving of the points and ask them to submit an "answer".  Or you can decide that no expert was deserving and delete the question.  The other option---if you have the points---is to have one expert answer here and get the points and ask a "dummy" question for the other expert to answer (with the expert's name on it.)
Dividing the points is not possible, I'm afraid. I suggest you ask nietod submit an answer, so you can give hime the points - his comments are the ones that actually helped you.
Avatar of sneeuw

ASKER

Ok,
Since Yonat approves, I suggest I give Nietod the 250 points.
Maybe Nietod can ask a dummy question that yonat can answer for 125 points !
So Nietod, submit your answer and I'll award you for it.

Regards,
Peter
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

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