Leaking C++ Exceptions into Delphi RTL

From RAD Studio
Jump to: navigation, search

Go Up to Standard C++ Exception Handling


When the Delphi RTL encounters an exception that is not Delphi generated or hardware related, the exception is mapped as an EExternalException. If C++ Exceptions are not explicitly caught, they are eventually caught by Delphi exception handlers, and therefore the application raises an External Exception EEFFACE.

C++ standard exceptions, as well as user-defined C++ exceptions, cannot be handled by the Delphi run time. This means that working with C++ libraries, such as the Standard Template Libraries (STL) or the Dinkumware Standard C++ Library, requires a bit more attention. If an exception is not explicitly caught and it leaks into the Delphi run time, the application raises an External Exception EEFFACE and crashes.

Example

You can find below a small example that shows how using STL libraries without paying attention to the exceptions raised by these libraries can lead to crashing applications.

This example is a VCL application that does some general operation on a vector of integers. The code is not checked for C++ exceptions raised by vector methods, so at some point the application crashes, raising an External Exception EEFFACE.

void __fastcall TForm1::Button3Click(TObject *Sender) {
	std::vector<int> vect;
	vect.push_back(112);
	vect.push_back(222);
	Label1->Caption = vect.at(2);
	// the code does not check for std::out_of_range exception
	// correct code will be  :
//	try {
//		Label1->Caption = vect.at(2);
//	}
//	catch (std::out_of_range& e) {
//		Label1->Caption = e.what();
//	}
}

See Also