Exception order

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Airslash
    New Member
    • Nov 2007
    • 221

    Exception order

    Hello,

    I have an issue with the order of Exceptions. Currently my application just dies instead of handeling the one Exception it was supposed to catch, so I added additional try-catch statements to handle the errors.

    Here is the outer part of the code

    Code:
    		// We have to supply a new image ID because on every drive we
    		// rewrite from 0.
    		try
    		{
    			try
    			{
    				Path = DriveToUse->WriteImage(&TmpId, FileFormat, Buffer);
    			}
    			catch(std::exception &ex)
    			{
    				// Log the error message.
    				ShowMessage("Exception got caught");
    //				Logger::getSingletonPtr()->writeMsg(Ex.Message, 0, LOG_ERROR, true);
    //				Logger::getSingletonPtr()->writeLogMsg(Ex.Message, 0, LOG_ERROR, true, TDateTime::CurrentDate());
    
    				// Update the RecordId to skip over the following disk.
    				// If this is the last disk, then the recordID needs to
    				// be set to 0, else to the JumpPoint of the disk.
    				if(mDrives.back() == DriveToUse)
    				{
    					RingBuffer::Instance()->changeRecNr(0);
    				}
    				else
    				{
    					long NewId = CalculatePreviousJumpPoint(DriveToUse) + DriveToUse->MaximumImages();
    					RingBuffer::Instance()->changeRecNr(NewId);
    				}
    
    				// Call ourselves again, and try a new disk.
    				Path = WriteAnalogueImage(FileFormat, Buffer);
    			}
    
    			// Return true.
    			return Path;
    			}
    		catch(...)
    		{
                ShowMessage("AMAGAD SOMETHING UNKNOWN HAPPENDED!");
            }
    	}
    This function calls the WriteImage method on my Drive object. That function uses a library to access the hardware we use for talking to Cameras.
    This is the code of that function

    Code:
    AnsiString IO::Drive::WriteImage(long* RecordId, long FileFormat, MIL_ID Buffer)
    {
    	// Call the MIL function to write the file.
    	// Save the complete path and return it later on.
    	AnsiString Path = AnsiString(DriveLetter()) + ":\\Images\\";
    	AnsiString Folder = AnsiString(IntToHex((int)(*RecordId / 0xFFF), 3)) + "\\";
    	AnsiString Filename = AnsiString(IntToHex((int)(*RecordId % 0xFFF), 3)) + ".jpg";
    
    	// Check if this directory exits, create it if it doesnt.
    	if(!DirectoryExists(Path + Folder))
    	{
    		ForceDirectories(Path + Folder);
    	}
    
    	// Export the buffer to the image.
    	// Try to save the data from the buffer to a file on the disk.
    	MbufExport((Path + Folder + Filename).c_str(), FileFormat, Buffer);
    
    	// Get the last error returned from the MIL system, this way we can check
    	// if the previous function executed properly.
    	long MilErrorCode = MappGetError(M_CURRENT, M_NULL);
    
    	// Check if an error occured. If the value is M_NULL_ERROR then no
    	// error occured with the last function call.
    	if(M_NULL_ERROR == MilErrorCode)
    	{
    		return (Path + Folder + Filename);
    	}
    	else
    	{
    		ShowMessage("ErrorCode returned = " + AnsiString(MilErrorCode) + " :: M_NULL_ERROR = " + AnsiString(M_NULL_ERROR));
    		// Writing failed. throw a new excption
    		// and mark ourselves as unusable.
    		SetUsable(false);
    
    		// Throw the exception so we can catch it in the manager.
    		throw new Exception("Failed to write the file to the disk. ErrorCode : " + AnsiString(MilErrorCode));
    	}
    }
    When I ran the code and pulled out a harddrive this is the errormessages I got, in sequence:

    - AMAGAD SOMETHING UNKNOWN HAPPENED
    - ErrorCode Returned = 58, M+NULL_ERROR = 0
    - Caught an Exception

    I don't understand why they come in this order....
    I expected the following order of messages:

    - ShowMessage()
    - Caught an Exception
    - AMAGAD.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Don't new the exception, not this

    throw new Exception("Fail ed to write the file to the disk. ErrorCode : " + AnsiString(MilE rrorCode));

    but this

    throw Exception("Fail ed to write the file to the disk. ErrorCode : " + AnsiString(MilE rrorCode));

    Because the compiler can not match Exception& and Exception* as the same type. You should be catching by reference (See More Effective C++ by Scott Meyers for the reasons) so you have to throw an object not a pointer.

    Comment

    • Airslash
      New Member
      • Nov 2007
      • 221

      #3
      aah cool,

      wil definitly remember that for my next version.
      At the moment I removed the exceptions and used pointer to fill the variables and have my functions return a bool to signal success or failure

      Comment

      Working...