ProteoWizard
Exception.hpp
Go to the documentation of this file.
1 //
2 // $Id: Exception.hpp 4976 2013-09-19 21:55:32Z donmarsh $
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6 //
7 // Copyright 2008 Spielberg Family Center for Applied Proteomics
8 // Cedars Sinai Medical Center, Los Angeles, California 90048
9 // Copyright 2008 Vanderbilt University - Nashville, TN 37232
10 //
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 //
15 // http://www.apache.org/licenses/LICENSE-2.0
16 //
17 // Unless required by applicable law or agreed to in writing, software
18 // distributed under the License is distributed on an "AS IS" BASIS,
19 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 // See the License for the specific language governing permissions and
21 // limitations under the License.
22 //
23 
24 #ifndef _EXCEPTION_HPP_
25 #define _EXCEPTION_HPP_
26 
27 
28 #include <stdexcept>
29 #include <string>
30 
31 
32 namespace pwiz {
33 namespace util {
34 
35 class usage_exception : public std::runtime_error
36 {
37  public: usage_exception(const std::string& usage) : std::runtime_error(usage) {}
38 };
39 
40 class user_error : public std::runtime_error
41 {
42  public: user_error(const std::string& what) : std::runtime_error(what) {}
43 };
44 
45 } // namespace util
46 } // namespace pwiz
47 
48 
49 // make debug assertions throw exceptions in MSVC
50 #ifdef _DEBUG
51 #include <crtdbg.h>
52 #include <iostream>
53 #include <locale>
54 #include <sstream>
55 
56 // preprocessed prototype of SetErrorMode so windows.h doesn't have to be included;
57 // this requires linking to the shared runtime but pwiz always does that on Windows
58 extern "C" __declspec(dllimport) unsigned int __stdcall SetErrorMode(unsigned int uMode);
59 
60 namespace {
61 
62 inline std::string narrow(const std::wstring& str)
63 {
64  std::ostringstream oss;
65  const std::ctype<wchar_t>& ctfacet = std::use_facet< std::ctype<wchar_t> >(oss.getloc());
66  for (size_t i=0; i < str.size(); ++i)
67  oss << ctfacet.narrow(str[i], 0);
68  return oss.str();
69 }
70 
71 inline int CrtReportHook(int reportType, char *message, int *returnValue)
72 {
73  if (reportType == _CRT_ERROR || reportType == _CRT_ASSERT)
74  throw std::runtime_error(message);
75  return 0;
76 }
77 
78 inline int CrtReportHookW(int reportType, wchar_t *message, int *returnValue)
79 {
80  if (reportType == _CRT_ERROR || reportType == _CRT_ASSERT)
81  throw std::runtime_error(narrow(message));
82  return 0;
83 }
84 
85 } // namespace
86 
87 struct ReportHooker
88 {
89  ReportHooker()
90  {
91  SetErrorMode(SetErrorMode(0) | 0x0002); // SEM_NOGPFAULTERRORBOX
92  _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
93  _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
94  _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, &CrtReportHook);
95  _CrtSetReportHookW2(_CRT_RPTHOOK_INSTALL, &CrtReportHookW);
96  }
97 
98  ~ReportHooker()
99  {
100  _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, &CrtReportHook);
101  _CrtSetReportHookW2(_CRT_RPTHOOK_REMOVE, &CrtReportHookW);
102  }
103 };
104 static ReportHooker reportHooker;
105 #endif // _DEBUG
106 
107 
108 // make Boost assertions throw exceptions
109 #if !defined(NDEBUG)
110 #define BOOST_ENABLE_ASSERT_HANDLER
111 #include <sstream>
112 namespace boost
113 {
114  inline void assertion_failed(char const * expr, char const * function, char const * file, long line) // user defined
115  {
116  std::ostringstream oss;
117  oss << "[" << file << ":" << line << "] Assertion failed: " << expr;
118  throw std::runtime_error(oss.str());
119  }
120 
121  inline void assertion_failed_msg(char const * expr, char const * msg, char const * function, char const * file, long line) // user defined
122  {
123  std::ostringstream oss;
124  oss << "[" << file << ":" << line << "] Assertion failed: " << expr << " (" << msg << ")";
125  throw std::runtime_error(oss.str());
126  }
127 } // namespace boost
128 #endif // !defined(NDEBUG)
129 
130 
131 #endif // _EXCEPTION_HPP_
STL namespace.
user_error(const std::string &what)
Definition: Exception.hpp:42
void assertion_failed_msg(char const *expr, char const *msg, char const *function, char const *file, long line)
Definition: Exception.hpp:121
usage_exception(const std::string &usage)
Definition: Exception.hpp:37
void assertion_failed(char const *expr, char const *function, char const *file, long line)
Definition: Exception.hpp:114