xtd 0.2.0
Loading...
Searching...
No Matches
application_and_exception.cpp

Shows how to create an application with xtd::forms::application class and how to manage exception.

Windows

macOS

Gnome

#include <xtd/forms/application>
#include <xtd/forms/button>
#include <xtd/forms/form>
#include <xtd/forms/message_box>
#include <xtd/operation_canceled_exception>
#include <xtd/startup>
#include <stdexcept>
using namespace std;
using namespace xtd;
using namespace xtd::forms;
class main_form : public form {
public:
static auto main() {
application::run(main_form());
}
main_form() {
text("application and exception example");
// uncomment next line to throw error and catch it in main entry point
//throw system_exception(current_stack_frame_);
generate_handled_exception_button.auto_size(true);
generate_handled_exception_button.location({10, 10});
generate_handled_exception_button.parent(*this);
generate_handled_exception_button.text("Generate handled exception");
generate_handled_exception_button.click += event_handler(*this, &main_form::generate_handled_exception);
generate_exception_button.auto_size(true);
generate_exception_button.location({10, 50});
generate_exception_button.parent(*this);
generate_exception_button.text("Generate exception");
generate_exception_button.click += event_handler(*this, &main_form::generate_exception);
generate_system_exception_button.auto_size(true);
generate_system_exception_button.location({10, 90});
generate_system_exception_button.parent(*this);
generate_system_exception_button.text("Generate system exception");
generate_system_exception_button.click += event_handler(*this, &main_form::generate_system_exception);
generate_unknown_exception_button.auto_size(true);
generate_unknown_exception_button.location({10, 130});
generate_unknown_exception_button.parent(*this);
generate_unknown_exception_button.text("Generate unknown exception");
generate_unknown_exception_button.click += event_handler(*this, &main_form::generate_unknown_exception);
}
private:
void generate_handled_exception() {
try {
} catch (const xtd::system_exception& e) {
message_box::show(*this, e.message(), ustring::format("Exception {} handled", e.name()));
}
}
void generate_exception() {throw invalid_argument("Invalid argument");}
void generate_system_exception() {throw argument_out_of_range_exception(current_stack_frame_);}
void generate_unknown_exception() {throw "Unknown exception occured";}
button generate_handled_exception_button;
button generate_exception_button;
button generate_system_exception_button;
button generate_unknown_exception_button;
};
startup_(main_form::main);
The exception that is thrown when one of the arguments provided to a method is out of range.
Definition argument_out_of_range_exception.h:20
Represents a Windows button control.
Definition button.h:47
Represents a window or dialog box that makes up an application's user interface.
Definition form.h:52
The exception that is thrown in a thread upon cancellation of an operation that the thread was execut...
Definition operation_canceled_exception.h:18
Defines the base class for predefined exceptions in the xtd namespace.
Definition system_exception.h:25
generic_event_handler<> event_handler
Represents the method that will handle an event that has no event data.
Definition event_handler.h:32
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:166
#define current_stack_frame_
Provides information about the current stack frame.
Definition current_stack_frame.h:16
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition xtd_about_box.h:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10