Add wxApp::StoreCurrentException() and RethrowStoredException().

These methods can be used to ensure that the exceptions thrown from event
handlers are safely rethrown from the code dispatching the events once the
control flow gets back there.

This allows to work around the problem with not being able to propagate
exceptions through non-C++ code and can be used, for example, to catch
exceptions thrown by the handlers invoked from inside wxYield() by a try/catch
block around wxYield() -- something that didn't work before, update the except
sample to show that it does work now.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77468 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2014-08-24 15:31:44 +00:00
parent c779385a1a
commit 1cecee5bb7
9 changed files with 333 additions and 22 deletions

View File

@@ -72,6 +72,40 @@ the user about the problem (while being careful not to throw any more
exceptions as otherwise @c std::terminate() will be called).
@section overview_exceptions_store_rethrow Handling Exception Inside wxYield()
In some, relatively rare cases, using wxApp::OnExceptionInMainLoop() may not
be sufficiently flexible. The most common example is using automated GUI tests,
when test failures are signaled by throwing an exception and these exceptions
can't be caught in a single central method because their handling depends on
the test logic, e.g. sometimes an exception is expected while at other times it
is an actual error. Typically this results in writing code like the following:
@code
void TestNewDocument()
{
wxUIActionSimulator ui;
ui.Char('n', wxMOD_CONTROL); // simulate creating a new file
// Let wxWidgets dispatch Ctrl+N event, invoke the handler and create the
// new document.
try {
wxYield();
} catch ( ... ) {
// Handle exceptions as failure in the new document creation test.
}
}
@endcode
Unfortunately, by default this example does @e not work because an exception
can't be safely propagated back to the code handling it in @c TestNewDocument()
through the system event dispatch functions which are not compatible with C++
exceptions. Because of this, you need to override wxApp::StoreCurrentException()
and wxApp::RethrowStoredException() to help wxWidgets to safely transport the
exception from the event handler that throws it to the @c catch clause. Please
see the documentation of these functions for more details.
@section overview_exceptions_tech Technicalities
To use any kind of exception support in the library you need to build it