xtd - Reference Guide  0.2.0
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 
30  using exception_ref = std::optional<std::reference_wrapper<const std::exception>>;
32 
34 
39  explicit system_exception(const xtd::diagnostics::stack_frame& information = xtd::diagnostics::stack_frame::empty()) : system_exception(default_message(), nullptr, std::error_code(), "", information) {}
43  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) {}
48  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) {}
53  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) {}
59  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) {}
64  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) {}
69  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) {}
75  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) {}
81  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) {}
88  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) {}
90 
92  system_exception(const system_exception&) = default;
93  system_exception& operator=(const system_exception&) = default;
95 
97 
101  virtual const xtd::ustring& file_path() const noexcept {return information_.get_file_name();}
102 
105  virtual const xtd::ustring& help_link() const noexcept {return help_link_;}
106 
108  static bool enable_stack_trace() {return enable_stack_trace_;}
109  static void enable_stack_trace(bool enable) {enable_stack_trace_ = enable;}
110 
113  virtual std::error_code error_code() const noexcept {return error_;}
114 
117  virtual exception_ref inner_exception() const noexcept {return inner_exception_;}
118 
121  virtual uint32_t line_number() const noexcept {return information_.get_file_line_number();}
122 
125  virtual const xtd::ustring& member_name() const noexcept {return information_.get_method();}
126 
129  virtual const xtd::ustring& message() const noexcept {return message_;}
130 
133  virtual const xtd::ustring& name() const noexcept {return (name_ = xtd::ustring::full_class_name(*this));}
134 
137  virtual xtd::ustring stack_trace() const noexcept {return stack_trace_to_string();}
139 
141 
145  xtd::ustring to_string() const noexcept override;
146 
148  friend std::ostream& operator<<(std::ostream& os, const xtd::system_exception& e) noexcept {
149  return os << e.to_string();
150  }
152 
155  const char* what() const noexcept override {return message().empty() ? name().c_str() : message().c_str();}
157 
158  private:
159  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);
160  xtd::ustring stack_trace_to_string() const noexcept;
161  const char* default_message() const;
162 
163  static bool enable_stack_trace_;
164  mutable xtd::ustring name_;
165  xtd::ustring message_;
166  exception_ref inner_exception_;
167  std::error_code error_;
168  xtd::ustring help_link_;
169  xtd::diagnostics::stack_frame information_;
170  std::shared_ptr<xtd::diagnostics::stack_trace> stack_trace_;
171  };
172 }
173 
186 #define system_exception_(...) system_exception(add_current_stack_frame_(__VA_ARGS__))
Contains xtd::diagnostics::stack_trace class.
Defines the base class for predefined exceptions in the xtd namespace.
Definition: system_exception.h:24
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:64
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Contains xtd literals.
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:48
virtual std::error_code error_code() const noexcept
Get error associate to the exception.
Definition: system_exception.h:113
virtual xtd::ustring stack_trace() const noexcept
Gets a string representation of the immediate frames on the call stack.
Definition: system_exception.h:137
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:53
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:50
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:59
virtual const xtd::ustring & member_name() const noexcept
Get file member where exception occurred.
Definition: system_exception.h:125
std::string to_string(const date_time &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition: date_time.h:1110
static bool enable_stack_trace()
Gets or sets if the generation of the stack trace is enabled.
Definition: system_exception.h:108
xtd::ustring to_string() const noexcept override
Returns a string that represents the current exception.
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:43
std::optional< std::reference_wrapper< const std::exception > > exception_ref
Optional reference wrapper on std::exception.
Definition: system_exception.h:30
virtual uint32_t line_number() const noexcept
Get Line number where the exception occurred.
Definition: system_exception.h:121
virtual exception_ref inner_exception() const noexcept
Gets the exception instance that caused the current exception.
Definition: system_exception.h:117
virtual const xtd::ustring & name() const noexcept
Get full class name the exception.
Definition: system_exception.h:133
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:88
Provides information about a xtd::diagnostics::stack_frame, which represents a function call on the c...
Definition: stack_frame.h:34
static stack_frame empty() noexcept
Return an empty stack frame.
Contains xtd::ustring class.
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:39
Contains xtd::object class.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:30
virtual const xtd::ustring & file_path() const noexcept
Get file path where exception occurred.
Definition: system_exception.h:101
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:444
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:69
const char * what() const noexcept override
Get message associate to the exception.
Definition: system_exception.h:155
virtual const xtd::ustring & message() const noexcept
Get message associate to the exception.
Definition: system_exception.h:129
Contains xtd::diagnostics::stack_frame class.
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:75
virtual const xtd::ustring & help_link() const noexcept
Gets a link to the help file associated with this exception.
Definition: system_exception.h:105
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:81