-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunix_error.h
More file actions
57 lines (52 loc) · 1.56 KB
/
unix_error.h
File metadata and controls
57 lines (52 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#ifndef UNIX_ERROR_H
#define UNIX_ERROR_H 1
/** @file unix_error.h
* The declaration of the unix_error exception class.
*/
// c++ hooks
#include <stdexcept> // std::runtime_error
#include <string> // std::string
/** @class unix_error
* A class to throw unix system errors with some additional text (derives from runtime_error).
*
* When somewhere in your program a systemcall fails,
* e.g. you can not open a file,
* the reason is explained as a code in the global 'errno' variable.
*
* This exception class takes that code,
* converting it into some human readable string,
* addding the given argument as a prefix.
* <br>Also see: errno, std::strerror() and std::perror()
*
* An example:
@code
try {
// With C++ STL classes
ifstream inputfile("some_filename"); // an ifstream
if (!inputfile) // failed to open?
throw unix_error("inputfile"); // report the problem
// With unix systemcalls
if (chdir("/tmp") < 0)
throw unix_error("chdir /tmp");
}
catch(const unix_error& e) {
cerr << "Oops: " << e.what() << endl;
}
@endcode
*
*/
class unix_error : public std::runtime_error
{
public:
explicit // see: http://en.cppreference.com/w/cpp/language/explicit
/// Construct a unix system error with some additional text
/// @param what_arg additional text
unix_error(const std::string& what_arg);
explicit // see: http://en.cppreference.com/w/cpp/language/explicit
/// Construct a unix system error with some additional text
/// @param what_arg additional text
unix_error(const char *what_arg);
};
#endif // UNIX_ERROR_H
// vim:sw=4:ts=4:ai:aw: