Vowpal Wabbit
vw_exception.h
Go to the documentation of this file.
1 /*
2 Copyright (c) by respective owners including Yahoo!, Microsoft, and
3 individual contributors. All rights reserved. Released under a BSD (revised)
4 license as described in the file LICENSE.
5 */
6 
7 #pragma once
8 #ifndef VW_NOEXCEPT
9 #include <stdexcept>
10 #include <sstream>
11 
12 #include <cstring>
13 
14 #ifdef _WIN32
15 #define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
16 #else
17 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
18 #endif
19 
20 namespace VW
21 {
22 class vw_exception : public std::exception
23 {
24  private:
25  // source file exception was thrown
26  const char* file;
27 
28  std::string message;
29 
30  // line number exception was thrown
32 
33  public:
34  vw_exception(const char* file, int lineNumber, std::string const& message) noexcept;
35  vw_exception(const vw_exception& ex) noexcept;
36  vw_exception& operator=(const vw_exception& other) noexcept;
37 
38  ~vw_exception() noexcept override;
39 
40  const char* what() const noexcept override;
41  const char* Filename() const;
42  int LineNumber() const;
43 };
44 
46 {
47  public:
48  vw_argument_disagreement_exception(const char* file, int lineNumber, const std::string& message)
49  : vw_exception(file, lineNumber, message)
50  {
51  }
52 
54 
56  {
57  // check for self-assignmentW
58  if (&other == this)
59  return *this;
61  return *this;
62  }
63 
64  ~vw_argument_disagreement_exception() noexcept override = default;
65 };
66 
68 {
69  public:
70  vw_argument_invalid_value_exception(const char* file, int lineNumber, const std::string& message)
71  : vw_exception(file, lineNumber, message)
72  {
73  }
74 
76 
78  {
79  // check for self-assignment
80  if (&other == this)
81  return *this;
83  return *this;
84  }
85 
86  ~vw_argument_invalid_value_exception() noexcept override = default;
87 };
88 
90 {
91  public:
92  vw_unrecognised_option_exception(const char* file, int lineNumber, const std::string& message)
93  : vw_exception(file, lineNumber, message)
94  {
95  }
96 
98 
100  {
101  // check for self-assignment
102  if (&other == this)
103  return *this;
105  return *this;
106  }
107 
108  ~vw_unrecognised_option_exception() noexcept override = default;
109 };
110 
112 {
113  public:
114  strict_parse_exception(const char* file, int lineNumber, const std::string& message)
115  : vw_exception(file, lineNumber, message)
116  {
117  }
118 
120 
122  {
123  // check for self-assignment
124  if (&other == this)
125  return *this;
127  return *this;
128  }
129 
130  ~strict_parse_exception() noexcept override = default;
131 };
132 
133 #ifdef _WIN32
134 void vw_trace(const char* filename, int linenumber, const char* fmt, ...);
135 
136 // useful when hunting down release mode bugs
137 #define VW_TRACE(fmt, ...) VW::vw_trace(__FILE__, __LINE__, fmt, __VA_ARGS__)
138 
139 struct StopWatchData;
140 
141 class StopWatch
142 {
143  StopWatchData* data;
144 
145  public:
146  StopWatch();
147  ~StopWatch();
148 
149  double MilliSeconds() const;
150 };
151 
152 // Equivalent to System::Diagnostics::Debugger::Launch();
153 bool launchDebugger();
154 
155 #define THROWERRNO(args) \
156  { \
157  std::stringstream __msg; \
158  __msg << args; \
159  char __errmsg[256]; \
160  if (strerror_s(__errmsg, sizeof __errmsg, errno) != 0) \
161  __msg << ", errno = unknown"; \
162  else \
163  __msg << ", errno = " << __errmsg; \
164  throw VW::vw_exception(__FILENAME__, __LINE__, __msg.str()); \
165  }
166 #else
167 #define THROWERRNO(args) \
168  { \
169  std::stringstream __msg; \
170  __msg << args; \
171  char __errmsg[256]; \
172  if (strerror_r(errno, __errmsg, sizeof __errmsg) != 0) \
173  __msg << "errno = unknown"; \
174  else \
175  __msg << "errno = " << __errmsg; \
176  throw VW::vw_exception(__FILENAME__, __LINE__, __msg.str()); \
177  }
178 #endif
179 
180 // ease error handling and also log filename and line number
181 #define THROW(args) \
182  { \
183  std::stringstream __msg; \
184  __msg << args; \
185  throw VW::vw_exception(__FILENAME__, __LINE__, __msg.str()); \
186  }
187 
188 #define THROW_EX(ex, args) \
189  { \
190  std::stringstream __msg; \
191  __msg << args; \
192  throw ex(__FILENAME__, __LINE__, __msg.str()); \
193  }
194 } // namespace VW
195 
196 #define VW_ASSERT(condition, args) \
197  if (!(condition)) \
198  { \
199  THROW(args); \
200  }
201 
202 #endif
203 
204 #define EXPAND(x) x
205 #define GET_MACRO(_1, _2, NAME, ...) NAME
206 // UNUSED is used to silence the warning: warning: ISO C++11 requires at least one argument for the "..." in a variadic
207 // macro
208 #define THROW_OR_RETURN(...) \
209  EXPAND(GET_MACRO(__VA_ARGS__, THROW_OR_RETURN_NORMAL, THROW_OR_RETURN_VOID, UNUSED)(__VA_ARGS__))
210 
211 #ifdef VW_NOEXCEPT
212 
213 #define THROW_OR_RETURN_NORMAL(args, retval) \
214  do \
215  { \
216  return retval; \
217  } while (0)
218 
219 #define THROW_OR_RETURN_VOID(args) \
220  do \
221  { \
222  return; \
223  } while (0)
224 
225 #else // VW_NOEXCEPT defined
226 
227 #define THROW_OR_RETURN_NORMAL(args, retval) \
228  do \
229  { \
230  std::stringstream __msgA; \
231  __msgA << args; \
232  throw VW::vw_exception(__FILE__, __LINE__, __msgA.str()); \
233  } while (0)
234 
235 #define THROW_OR_RETURN_VOID(args) \
236  do \
237  { \
238  std::stringstream __msgB; \
239  __msgB << args; \
240  throw VW::vw_exception(__FILE__, __LINE__, __msgB.str()); \
241  } while (0)
242 
243 #endif
244 #define _UNUSED(x) ((void)(x))
245 
246 #define DBG(x) \
247  do \
248  { \
249  std::cerr << "(" << __FILENAME__ << ":" << __LINE__ << "," << __func__ << ") " << #x << ": " << x << std::endl; \
250  } while (0)
strict_parse_exception(const char *file, int lineNumber, const std::string &message)
Definition: vw_exception.h:114
strict_parse_exception(const strict_parse_exception &ex)
Definition: vw_exception.h:119
vw_argument_disagreement_exception(const char *file, int lineNumber, const std::string &message)
Definition: vw_exception.h:48
vw_argument_disagreement_exception & operator=(const vw_argument_disagreement_exception &other)
Definition: vw_exception.h:55
vw_argument_invalid_value_exception(const char *file, int lineNumber, const std::string &message)
Definition: vw_exception.h:70
vw_unrecognised_option_exception(const vw_unrecognised_option_exception &ex)
Definition: vw_exception.h:97
const char * Filename() const
Definition: vw_exception.cc:37
const char * what() const noexcept override
Definition: vw_exception.cc:35
vw_unrecognised_option_exception(const char *file, int lineNumber, const std::string &message)
Definition: vw_exception.h:92
vw_argument_invalid_value_exception(const vw_argument_invalid_value_exception &ex)
Definition: vw_exception.h:75
vw_unrecognised_option_exception & operator=(const vw_unrecognised_option_exception &other)
Definition: vw_exception.h:99
vw_exception & operator=(const vw_exception &other) noexcept
Definition: vw_exception.cc:20
std::string message
Definition: vw_exception.h:28
~vw_exception() noexcept override
const char * file
Definition: vw_exception.h:26
vw_argument_invalid_value_exception & operator=(const vw_argument_invalid_value_exception &other)
Definition: vw_exception.h:77
vw_exception(const char *file, int lineNumber, std::string const &message) noexcept
Definition: vw_exception.cc:10
Definition: autolink.cc:11
strict_parse_exception & operator=(const strict_parse_exception &other)
Definition: vw_exception.h:121
int LineNumber() const
Definition: vw_exception.cc:39
vw_argument_disagreement_exception(const vw_argument_disagreement_exception &ex)
Definition: vw_exception.h:53