| 1 |
#include <iostream> |
| 2 |
#include <gtkmm/box.h> |
| 3 |
#include <glibmm/convert.h> |
| 4 |
#include <glibmm/exceptionhandler.h> |
| 5 |
#include <gtkmm/application.h> |
| 6 |
#include <gtkmm/window.h> |
| 7 |
#include <gtkmm/button.h> |
| 8 |
|
| 9 |
namespace |
| 10 |
{ |
| 11 |
class MyButton : public Gtk::Button |
| 12 |
{ |
| 13 |
public: |
| 14 |
explicit MyButton(const Glib::ustring& label); |
| 15 |
|
| 16 |
protected: |
| 17 |
void on_clicked() override; // signal handler |
| 18 |
}; |
| 19 |
|
| 20 |
class MyException1 {}; |
| 21 |
class MyException2 {}; |
| 22 |
class MyException3 {}; |
| 23 |
|
| 24 |
MyButton::MyButton(const Glib::ustring& label) |
| 25 |
: |
| 26 |
Gtk::Button(label) |
| 27 |
{} |
| 28 |
|
| 29 |
void MyButton::on_clicked() |
| 30 |
{ |
| 31 |
std::cout << "The Button was clicked.\n"; |
| 32 |
throw MyException1(); |
| 33 |
} |
| 34 |
|
| 35 |
void global_on_clicked() |
| 36 |
{ |
| 37 |
std::cout << "The Button was clicked.\n"; |
| 38 |
throw MyException2(); |
| 39 |
} |
| 40 |
|
| 41 |
void global_on_clicked_throw_glib_error() |
| 42 |
{ |
| 43 |
std::cout << "The Button was clicked.\n"; |
| 44 |
(void)Glib::locale_from_utf8("Und tsch\xfcss!"); // invalid UTF-8 |
| 45 |
} |
| 46 |
|
| 47 |
void global_on_clicked_throw_std_exception() |
| 48 |
{ |
| 49 |
std::cout << "The Button was clicked.\n"; |
| 50 |
Glib::ustring str; |
| 51 |
str.at(3); // out of range |
| 52 |
} |
| 53 |
|
| 54 |
void handler1() |
| 55 |
{ |
| 56 |
std::cout << "exception handler 1\n"; |
| 57 |
|
| 58 |
try |
| 59 |
{ |
| 60 |
throw; // rethrow exception |
| 61 |
} |
| 62 |
catch(MyException1&) |
| 63 |
{ |
| 64 |
std::cout << "caught MyException1\n"; |
| 65 |
} |
| 66 |
// if not handled it gets passed to next handler |
| 67 |
} |
| 68 |
|
| 69 |
void handler2() |
| 70 |
{ |
| 71 |
std::cout << "exception handler 2\n"; |
| 72 |
|
| 73 |
try |
| 74 |
{ |
| 75 |
throw; // rethrow exception |
| 76 |
} |
| 77 |
catch(MyException2&) |
| 78 |
{ |
| 79 |
std::cout << "caught MyException2\n"; |
| 80 |
} |
| 81 |
// if not handled it gets passed to next handler |
| 82 |
} |
| 83 |
|
| 84 |
class MyWindow : public Gtk::Window |
| 85 |
{ |
| 86 |
public: |
| 87 |
MyWindow(); |
| 88 |
private: |
| 89 |
sigc::connection conn_handler1; |
| 90 |
sigc::connection conn_handler2; |
| 91 |
}; |
| 92 |
|
| 93 |
MyWindow::MyWindow() |
| 94 |
{ |
| 95 |
set_title("Exception Handlers"); |
| 96 |
|
| 97 |
auto box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 5); |
| 98 |
box->set_margin(10); |
| 99 |
set_child(*box); |
| 100 |
|
| 101 |
// New exception handlers are inserted at the back of the per-thread |
| 102 |
// handler list. Thus, in this example handler2() is executed after handler1(). |
| 103 |
conn_handler1 = Glib::add_exception_handler(&handler1); |
| 104 |
conn_handler2 = Glib::add_exception_handler(&handler2); |
| 105 |
|
| 106 |
box->append(*Gtk::make_managed<MyButton>("From virtual method")); |
| 107 |
auto button2 = Gtk::make_managed<Gtk::Button>("From signal handler"); |
| 108 |
box->append(*button2); |
| 109 |
auto button3 = Gtk::make_managed<Gtk::Button>("Unhandled Glib::Error"); |
| 110 |
box->append(*button3); |
| 111 |
auto button4 = Gtk::make_managed<Gtk::Button>("Unhandled std::exception (crash!)"); |
| 112 |
box->append(*button4); |
| 113 |
auto button5 = Gtk::make_managed<Gtk::Button>("Disconnect handler 1"); |
| 114 |
box->append(*button5); |
| 115 |
auto button6 = Gtk::make_managed<Gtk::Button>("Disconnect handler 2"); |
| 116 |
box->append(*button6); |
| 117 |
|
| 118 |
button2->signal_clicked().connect(&global_on_clicked); |
| 119 |
button3->signal_clicked().connect(&global_on_clicked_throw_glib_error); |
| 120 |
button4->signal_clicked().connect(&global_on_clicked_throw_std_exception); |
| 121 |
button5->signal_clicked().connect(sigc::mem_fun(conn_handler1, &sigc::connection::disconnect)); |
| 122 |
button6->signal_clicked().connect(sigc::mem_fun(conn_handler2, &sigc::connection::disconnect)); |
| 123 |
} |
| 124 |
} // anonymous namespace |
| 125 |
|
| 126 |
int main(int argc, char** argv) |
| 127 |
{ |
| 128 |
auto app = Gtk::Application::create("org.gtkmm.example"); |
| 129 |
return app->make_window_and_run<MyWindow>(argc, argv); |
| 130 |
} |