Throw Catch for External Program

This is a discussion on Throw Catch for External Program within the C++ Programming forums, part of the General Programming Boards category; Is it possible to catch errors of an external program launched within runtime? Let's say for example I'm writing to ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    9

    Throw Catch for External Program

    Is it possible to catch errors of an external program launched within runtime?
    Let's say for example I'm writing to a file and the file doesn't have enough contents to be "openable" so when launching the program giving the filename as a parameter I get an error. Later on processing is done and the file becomes launchable.

    How can I detect this? Do I have to rely on a fixed file size (checking for a particular file size) or is there a way how to do this dynamically within runtime, i.e if file is not openable try reopening file after some other events have occurred (example events that are filling the file with content)

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by dav_mt View Post
    Is it possible to catch errors of an external program launched within runtime?
    Debuggers do it, so there must be a way.

    Quote Originally Posted by dav_mt View Post
    Let's say for example I'm writing to a file and the file doesn't have enough contents to be "openable" so when launching the program giving the filename as a parameter I get an error. Later on processing is done and the file becomes launchable.

    How can I detect this? Do I have to rely on a fixed file size (checking for a particular file size) or is there a way how to do this dynamically within runtime, i.e if file is not openable try reopening file after some other events have occurred (example events that are filling the file with content)
    I'm not sure what you mean? I can open a file whether it's 0 bytes or 1GB, the only thing that would make it not "openable" is if the file is locked by another process.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    If you're launching the program with a system() call, the best you can get is a return code from the O/S. You can't catch exceptions.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    my issue is a little bit different here.

    I'm sending video packets over a network and I'm launching Real Player to decode the file. If the first intra-frame of the file and the header of the video file has not yet arrived Real Player will refuse to start decoding and will give a CORRUPT FILE error.

    What I want to do is trap the error given by the player and retry opening the file after some more video packets have arrived (i have track of what video packets I'm receiving).

    Is this actually possible?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    How are you launching Real Player?
    Using createProcess perhaps?

    > Real Player will refuse to start decoding and will give a CORRUPT FILE error.
    Now you're at the mercy of the people who wrote the program. If they did a decent job, then this exit status may be available to you, see http://msdn2.microsoft.com/en-us/lib...58(VS.85).aspx
    But if they're your typical "void main" programmer, then you're more likely to end up with either zero (or worse, rubbish) as the exit status.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    Code:
    if (packetsrcvd == 250)	//850
    			{	
    				pID = fork();
       			if (pID == 0)
       			{	
    					gettimeofday(&end, NULL);
    		
    					cout << "Received after: " << endl;
    					if (end.tv_usec > start.tv_usec)
      					{
      					cout << end.tv_sec - start.tv_sec << ".";
      					cout << end.tv_usec - start.tv_usec << "s" << endl;
      					}
      					else if (end.tv_usec < start.tv_usec)
      					{
      					cout << end.tv_sec - start.tv_sec - 1 << ".";
      					cout << start.tv_usec - end.tv_usec + 1 << "s" << endl;
     					}
    
    					long counter = 0;
    					cout << "\nProcess created succesfully...";
    					cout << "\nPackets received = " << packetsrcvd;
    					cout << "\nYou have received " << rx << " bytes.";
    					execl("/opt/real/RealPlayer/realplay", "-o", "video_in.3gp", (char *) 0);
        			}
    			}
    I'm programming in a unix environment. My aim is to start the player immediately without waiting for 250 packets to be received.
    Given not enough data is received I get,
    General error: HXR_CORRUPT_FILE (0x80040091) (file:///home/user/BTStream/BTStream/src/video_in.3gp) from the video player, but I'm doubtful if this error can be captured to the enviroment.
    The reason I want to do this is to get the exact value and time at which I can start decoding.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > execl("/opt/real/RealPlayer/realplay", "-o", "video_in.3gp", (char *) 0);
    Shouldn't this be
    Code:
    execl("/opt/real/RealPlayer/realplay", 
          "/opt/real/RealPlayer/realplay", "-o", "video_in.3gp", (char *) 0);
    The second parameter of the exec call will be argv[0]
    http://www.rootr.net/man/man/execl/3



    Code:
    if (pID == 0) {
    } else
    if ( pID != (pid_t)-1 ) {
      int status;
      waitpid( pID, &status, 0 );
    } else {
      perror("Failed to fork");
    }
    http://www.rootr.net/man/man/wait/2
    Use the various 'W' macros to determine the exact reason for your child process ending.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. how to throw a windows program into the taskbar on starting?
    By Leeman_s in forum Windows Programming
    Replies: 4
    Last Post: 12-17-2002, 09:55 PM
  5. Replies: 2
    Last Post: 05-10-2002, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21