TextException.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID__TEXTEXCEPTION_H
10 #define SQUID__TEXTEXCEPTION_H
11 
12 #include "base/Here.h"
13 
14 #include <stdexcept>
15 
16 class SBuf;
17 
19 class TextException: public std::runtime_error
20 {
21 
22 public:
23  TextException(const char *message, const SourceLocation &location):
24  std::runtime_error(message),
25  where(location)
26  {}
27 
28  TextException(SBuf message, const SourceLocation &location);
29 
30  TextException(const TextException &) = default;
31  TextException(TextException &&) = default;
32  TextException& operator=(const TextException &) = default;
33 
34  /* std::runtime_error API */
35  virtual ~TextException() throw() override;
36  virtual const char *what() const throw() override;
37 
39  SourceLocationId id() const { return where.id(); }
40 
42  std::ostream &print(std::ostream &) const;
43 
46 
47  // TODO: Add support for arbitrary (re)thrower-supplied details:
48  // std::tuple<Details...> details;
49 };
50 
52 std::ostream &CurrentException(std::ostream &);
53 
55 #define TexcHere(msg) TextException((msg), Here())
56 
59 #define Must2(condition, message) \
60  do { \
61  if (!(condition)) { \
62  const TextException Must_ex_((message), Here()); \
63  debugs(0, 3, Must_ex_.what()); \
64  throw Must_ex_; \
65  } \
66  } while (/*CONSTCOND*/ false)
67 
69 #define Must(condition) Must2((condition), "check failed: " #condition)
70 
73 #define SWALLOW_EXCEPTIONS(code) \
74  try { \
75  code \
76  } catch (...) { \
77  debugs(0, DBG_IMPORTANT, "BUG: ignoring exception;\n" << \
78  " bug location: " << Here() << "\n" << \
79  " ignored exception: " << CurrentException); \
80  }
81 
82 #endif /* SQUID__TEXTEXCEPTION_H */
83 
SourceLocationId id() const
Definition: Here.cc:67
std::ostream & print(std::ostream &) const
dumps the exception text into the stream
Definition: SBuf.h:86
TextException(const char *message, const SourceLocation &location)
Definition: TextException.h:23
TextException & operator=(const TextException &)=default
uint32_t SourceLocationId
semi-uniquely identifies a source code location; stable across Squid runs
Definition: Here.h:18
virtual const char * what() const override
SourceLocation where
code location related to the exception; usually the thrower location
Definition: TextException.h:45
virtual ~TextException() override
std::ostream & CurrentException(std::ostream &os)
prints active (i.e., thrown but not yet handled) exception
an std::runtime_error with thrower location info
Definition: TextException.h:19
a source code location that is cheap to create, copy, and store
Definition: Here.h:29

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors