LastOSError (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This code demonstrates the use of system error codes and how to transform them into C++ exceptions.

Code

void __fastcall TForm2::btRaiseLastClick(TObject *Sender)
{
	/* Set the last OS error to a bogus value. */
	System::SetLastError(ERROR_ACCESS_DENIED);

	try
	{
		RaiseLastOSError();
	}
	catch(EOSError* ex)
	{
		MessageDlg(String("Caught an OS error with code: ") +
			IntToStr((int)ex->ErrorCode), mtError,
			TMsgDlgButtons() << mbOK, 0);
	}


	/* Let the Delphi Exception dialog appear. */
	RaiseLastOSError(ERROR_NOT_ENOUGH_MEMORY);

	/* Finally, set the last error to none. */
	System::SetLastError(ERROR_SUCCESS);

	if (GetLastError() != ERROR_SUCCESS)
		MessageDlg(String("Whoops, something went wrong ") +
		  " in the mean time!", mtError, TMsgDlgButtons() << mbOK, 0);

	/* No exception should be thrown here. */
	RaiseLastOSError();
}

Uses