Voxel
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
windows_exception.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef WINDOWS_EXCEPTION_H
4 #define WINDOWS_EXCEPTION_H
5 
6 
7 /**
8  \file
9  \brief Contains WindowsException class.
10 */
11 
12 
13 
14 
15 #include <winerror.h>
16 
17 #include <string>
18 #include <stdexcept>
19 
20 
21 
22 
23 /*==============================================================================
24  THROW_WINDOWS_EXCEPTION macro
25 ==============================================================================*/
26 
27 
28 /// \brief Throws a WindowsException labeled with the file, line, string \p what and \c HRESULT error code \p hResult.
29 #define THROW_WINDOWS_EXCEPTION( what, hResult ) \
30  throw ::WindowsException( __FILE__, __LINE__, ( what ), ( hResult ) )
31 
32 
33 /// \brief Evaluates \p what, and if it returns an \c HRESULT failure code, throws a WindowsException.
34 #define CHECK_HRESULT( what ) { \
35  HRESULT result = ( what ); \
36  if ( FAILED( result ) ) \
37  THROW_WINDOWS_EXCEPTION( #what, result ); \
38 }
39 
40 
41 
42 
43 /*==============================================================================
44  WindowsException class
45 ==============================================================================*/
46 
47 
48 /**
49  \brief Windows exception class.
50 
51  WindowsExceptions should not be constructed explicitly. Instead, use the
52  THROW_WINDOWS_EXCEPTION() macro, which will construct and throw an
53  exception annotated with the filename and line number of the failure.
54 
55  The difference between this class and the "default" exception class is that
56  WindowsExceptions convert \c HRESULT error codes into error strings.
57 
58  The error message returned by what() has the following components:
59 
60  1. Filename and line number at which the exception was thrown.
61  2. Description string \p what passed to the constructor.
62  3. Description of the \c HRESULT error code passed to the constructor.
63 */
64 struct WindowsException : public std::exception {
65 
66  WindowsException( char const* const file, unsigned int const line, std::string const& what, HRESULT const hResult );
67 
68 
69  virtual ~WindowsException() throw();
70 
71 
72  virtual const char* what() const throw(); ///< Returns text (UTF-8) error message for this exception.
73 
74 
75 private:
76 
77  std::string m_what;
78 };
79 
80 
81 
82 
83 #endif // WINDOWS_EXCEPTION_H