xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
system_exception.h
Go to the documentation of this file.
1 #pragma once
5 #include <cstdint>
6 #include <optional>
7 #include <stdexcept>
8 #include <system_error>
9 #include "core_export.h"
10 #include "literals.h"
11 #include "object.h"
12 #include "ustring.h"
15 
17 namespace xtd {
24  class core_export_ system_exception : public object, public std::exception {
25  public:
27  using exception_ref = std::optional<std::reference_wrapper<const std::exception>>;
28 
32  explicit system_exception(const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(default_message(), nullptr, std::error_code(), "", information) {}
36  explicit system_exception(const xtd::ustring& message, const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(message, nullptr, std::error_code(), "", information) {}
41  system_exception(const xtd::ustring& message, const std::error_code& error, const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(message, nullptr, error, "", information) {}
46  system_exception(const xtd::ustring& message, const xtd::ustring& help_link, const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(message, nullptr, std::error_code(), help_link, information) {}
52  system_exception(const xtd::ustring& message, const std::error_code& error, const xtd::ustring& help_link, const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(message, nullptr, error, help_link, information) {}
57  explicit system_exception(const std::exception& inner_exception, const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(default_message(), &inner_exception, std::error_code(), "", information) {}
62  system_exception(const xtd::ustring& message, const std::exception& inner_exception, const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(message, &inner_exception, std::error_code(), "", information) {}
68  system_exception(const xtd::ustring& message, const std::exception& inner_exception, const std::error_code& error, const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(message, &inner_exception, error, "", information) {}
74  system_exception(const xtd::ustring& message, const std::exception& inner_exception, const xtd::ustring& help_link, const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(message, &inner_exception, std::error_code(), help_link, information) {}
81  system_exception(const xtd::ustring& message, const std::exception& inner_exception, const std::error_code& error, const xtd::ustring& help_link, const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(message, &inner_exception, error, help_link, information) {}
82 
84  system_exception(const system_exception&) = default;
85  system_exception& operator=(const system_exception&) = default;
87 
90  virtual const xtd::ustring& file_path() const noexcept {return information_.get_file_name();}
91 
94  virtual const xtd::ustring& help_link() const noexcept {return help_link_;}
95 
97  static bool enable_stack_trace() {return enable_stack_trace_;}
98  static void enable_stack_trace(bool enable) {enable_stack_trace_ = enable;}
99 
102  virtual std::error_code error_code() const noexcept {return error_;}
103 
106  virtual exception_ref inner_exception() const noexcept {return inner_exception_;}
107 
110  virtual uint32_t line_number() const noexcept {return information_.get_file_line_number();}
111 
114  virtual const xtd::ustring& member_name() const noexcept {return information_.get_method();}
115 
118  virtual const xtd::ustring& message() const noexcept {return message_;}
119 
122  virtual const xtd::ustring& name() const noexcept {return (name_ = xtd::ustring::full_class_name(*this));}
123 
126  virtual xtd::ustring stack_trace() const noexcept {return stack_trace_to_string();}
127 
130  xtd::ustring to_string() const noexcept override;
131 
133  friend std::ostream& operator<<(std::ostream& os, const xtd::system_exception& e) noexcept {
134  return os << e.to_string();
135  }
137 
140  const char* what() const noexcept override {return message().empty() ? name().c_str() : message().c_str();}
141 
142  private:
143  system_exception(const xtd::ustring& message, const std::exception* inner_exception, const std::error_code& error, const xtd::ustring& help_link, const xtd::diagnostics::stack_frame& information);
144  xtd::ustring stack_trace_to_string() const noexcept;
145  const char* default_message() const;
146 
147  static bool enable_stack_trace_;
148  mutable xtd::ustring name_;
149  xtd::ustring message_;
150  exception_ref inner_exception_;
151  std::error_code error_;
152  xtd::ustring help_link_;
153  xtd::diagnostics::stack_frame information_;
154  std::shared_ptr<xtd::diagnostics::stack_trace> stack_trace_;
155  };
156 }
157 
170 #define system_exception_(...) system_exception(add_current_stack_frame_(__VA_ARGS__))
Provides information about a xtd::diagnostics::stack_frame, which represents a function call on the c...
Definition: stack_frame.h:29
static stack_frame empty() noexcept
Return an empty stack frame.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition: object.h:26
Defines the base class for predefined exceptions in the xtd namespace.
Definition: system_exception.h:24
system_exception(const xtd::ustring &message, const std::exception &inner_exception, const std::error_code &error, const xtd::ustring &help_link, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:81
system_exception(const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:32
virtual const xtd::ustring & help_link() const noexcept
Gets a link to the help file associated with this exception.
Definition: system_exception.h:94
virtual const xtd::ustring & file_path() const noexcept
Get file path where exception occurred.
Definition: system_exception.h:90
static bool enable_stack_trace()
Gets or sets if the generation of the stack trace is enabled.
Definition: system_exception.h:97
virtual const xtd::ustring & member_name() const noexcept
Get file member where exception occurred.
Definition: system_exception.h:114
virtual uint32_t line_number() const noexcept
Get Line number where the exception occurred.
Definition: system_exception.h:110
system_exception(const xtd::ustring &message, const std::error_code &error, const xtd::ustring &help_link, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:52
system_exception(const xtd::ustring &message, const xtd::ustring &help_link, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:46
system_exception(const xtd::ustring &message, const std::exception &inner_exception, const xtd::ustring &help_link, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:74
virtual xtd::ustring stack_trace() const noexcept
Gets a string representation of the immediate frames on the call stack.
Definition: system_exception.h:126
std::optional< std::reference_wrapper< const std::exception > > exception_ref
Optional reference wrapper on std::exception.
Definition: system_exception.h:27
system_exception(const xtd::ustring &message, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:36
system_exception(const xtd::ustring &message, const std::exception &inner_exception, const std::error_code &error, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:68
system_exception(const std::exception &inner_exception, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:57
xtd::ustring to_string() const noexcept override
Returns a string that represents the current exception.
virtual const xtd::ustring & name() const noexcept
Get full class name the exception.
Definition: system_exception.h:122
const char * what() const noexcept override
Get message associate to the exception.
Definition: system_exception.h:140
system_exception(const xtd::ustring &message, const std::exception &inner_exception, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:62
virtual std::error_code error_code() const noexcept
Get error associate to the exception.
Definition: system_exception.h:102
system_exception(const xtd::ustring &message, const std::error_code &error, const xtd::diagnostics::stack_frame &information=xtd::diagnostics::stack_frame::empty())
Create a new instance of class system_exception.
Definition: system_exception.h:41
virtual const xtd::ustring & message() const noexcept
Get message associate to the exception.
Definition: system_exception.h:118
virtual exception_ref inner_exception() const noexcept
Gets the exception instance that caused the current exception.
Definition: system_exception.h:106
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
static ustring full_class_name()
Gets the fully qualified class name of the objec_t, including the namespace of the objec_t.
Definition: ustring.h:427
Contains core_export_ keyword.
#define core_export_
Define shared library export.
Definition: core_export.h:13
@ e
The E key.
Contains xtd literals.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::object class.
Contains xtd::diagnostics::stack_frame class.
Contains xtd::diagnostics::stack_trace class.
Contains xtd::ustring class.