-
Notifications
You must be signed in to change notification settings - Fork 7
User Guide : Exception Handling
Ruby standard library and application code commonly uses raising exceptions as a way to signal erroneous conditions like
too few/many arguments, type errors etc. Application code can either catch (rescue
) such exceptions and attempt
corrective procedures and/or gracefully handle shutdowns or it can let the exception propagate to be (possibly) caught
somewhere else up the call stack (possibly leading to the default behaviour when not caught, i.e. program exit with a
printed stack trace).
The C++ wxWidgets library on the other hand does not use or handle any exceptions (which is probably mostly due to the fact that some of the UI frameworks it encapsulates and integrates with do not either). Propagating Ruby exceptions (as C++ exceptions) from Ruby code called from the wxRuby C++ wrapper code is therefor a bad idea as the wxWidgets code does not expect any which may lead to unexpected results and often even segfaults.
The wxRuby extension library code will therefor protect against Ruby exceptions 'leaking' from called Ruby code and
prevent propagation to the wrapped wxWidgets C++ code. Instead any 'leaked' exceptions caught will be reported (standard
Ruby exception error message format with call stack) and an application exit will be forced with non-zero exit code
(i.e. a 'fail fast' strategy is implemented).
The wxRuby library should (!) be completely exception resistant, i.e. raised exceptions leaking from Ruby application code
should not propagate into the wrapped wxWidgets C++ library code and cause unexpected exception handling.
As far as handling application code exceptions is concerned the same advice applies as for wxWidgets itself; do NOT let exceptions escape your event handlers (as well as from any Wx class method overloads as these may also be called from the extension library's C++ wrapper code) meaning that if you can reasonably expect application code to raise exceptions you should make sure to catch any such exceptions within the context of the event handler (or overload) like:
class MyForm < Wx::Frame
def initialize(title)
super(nil, title: title)
# initialize frame elements
# ...
# setup event handlers
evt_menu MY_MENU_ID, :on_my_menu_item
# ...
end
def on_my_menu_item(evt)
begin
# execute some application code
# ...
rescue SomeException => ex
# handle exception
end
end
#...
end
-
- Introduction
- Quick start
- wxRuby Modules
- wxRuby Life Cycles
- wxRuby Dialogs
- wxRuby Enum values
- wxRuby Event Handling
- Geometry
- Colour and Font
- wxRuby Extensions
- Exception Handling
- Locating and loading art
- Drawing and Device Contexts
- Client data with wxRuby
- Validators and data binding
- Configuration support
- Persistence support