exceptions.h 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: exceptions.h
  3. // Purpose: topic overview
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @page overview_exceptions C++ Exceptions
  9. @tableofcontents
  10. wxWidgets had been started long before the exceptions were introduced in C++ so
  11. it is not very surprising that it is not built around using them as some more
  12. modern C++ libraries are. For instance, the library doesn't throw exceptions to
  13. signal about the errors. Moreover, up to (and including) the version 2.4 of
  14. wxWidgets, even using the exceptions in the user code was dangerous because the
  15. library code wasn't exception-safe and so an exception propagating through it
  16. could result in memory and/or resource leaks, and also not very convenient.
  17. However the recent wxWidgets versions are exception-friendly. This means that
  18. while the library still doesn't use the exceptions by itself, it should be now
  19. safe to use the exceptions in the user code and the library tries to help you
  20. with this.
  21. @section overview_exceptions_strategies Strategies for Exception Handling
  22. There are several choice for using the exceptions in wxWidgets programs. First
  23. of all, you may not use them at all. As stated above, the library doesn't throw
  24. any exceptions by itself and so you don't have to worry about exceptions at all
  25. unless your own code throws them. This is, of course, the simplest solution but
  26. may be not the best one to deal with all possible errors.
  27. The next simplest strategy is to only use exceptions inside non-GUI code, i.e.
  28. never let unhandled exceptions escape the event handler in which it happened.
  29. In this case using exceptions in wxWidgets programs is not different from using
  30. them in any other C++ program.
  31. Things get more interesting if you decide to let (at least some) exceptions
  32. escape from the event handler in which they occurred. Such exceptions will be
  33. caught by wxWidgets and the special wxApp::OnExceptionInMainLoop() method will
  34. be called from the @c catch clause. This allows you to decide in a single place
  35. what to do about such exceptions: you may want to handle the exception somehow
  36. or terminate the program. In this sense, OnExceptionInMainLoop() is equivalent
  37. to putting a @c try/catch block around the entire @c main() function body in
  38. the traditional console programs. However notice that, as its name indicates,
  39. this method won't help you with the exceptions thrown before the main loop is
  40. started or after it is over, so you may still want to have @c try/catch in your
  41. overridden wxApp::OnInit() and wxApp::OnExit() methods too, otherwise
  42. wxApp::OnUnhandledException() will be called.
  43. Finally, notice that even if you decide to not let any exceptions escape in
  44. this way, this still may happen unexpectedly in a program using exceptions as a
  45. result of a bug. So consider always overriding OnExceptionInMainLoop() in your
  46. wxApp-derived class if you use exceptions in your program, whether you expect
  47. it to be called or not. In the latter case you may simple re-throw the
  48. exception and let it bubble up to OnUnhandledException() as well.
  49. To summarize, when you use exceptions in your code, you may handle them in the
  50. following places, in order of priority:
  51. -# In a @c try/catch block inside an event handler.
  52. -# In wxApp::OnExceptionInMainLoop().
  53. -# In wxApp::OnUnhandledException().
  54. In the first two cases you may decide whether you want to handle the exception
  55. and continue execution or to exit the program. In the last one the program is
  56. about to exit already so you can just try to save any unsaved data and notify
  57. the user about the problem (while being careful not to throw any more
  58. exceptions as otherwise @c std::terminate() will be called).
  59. @section overview_exceptions_tech Technicalities
  60. To use any kind of exception support in the library you need to build it
  61. with @c wxUSE_EXCEPTIONS set to 1. It is turned on by default but you may
  62. wish to check @c include/wx/msw/setup.h file under Windows or run @c configure
  63. with explicit @c --enable-exceptions argument under Unix.
  64. On the other hand, if you do not plan to use exceptions, setting this
  65. flag to 0 or using @c --disable-exceptions could result in a leaner and
  66. slightly faster library.
  67. As for any other library feature, there is a sample (@c except)
  68. showing how to use it. Please look at its sources for further information.
  69. */