Open Chinese Convert: Exception.hpp Source File

Open Chinese Convert 1.0.6 A project for conversion between Traditional and Simplified Chinese

Exception.hpp
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2014 BYVoid <byvoid@byvoid.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #pragma once
20
21 #include <sstream>
22 #include <stdexcept>
23 #include <string>
24
25 #include "Export.hpp"
26
27 #if defined(_MSC_VER) && _MSC_VER < 1900
28 // Before Visual Studio 2015 (14.0), C++ 11 "noexcept" qualifier is not supported
29 #define noexcept
30 #endif // ifdef _MSC_VER
31
32 namespace opencc {
33
34 class OPENCC_EXPORT Exception {
35 public:
36 Exception() {}
37
38 virtual ~Exception() throw() {}
39
40 Exception(const std::string& _message) : message(_message) {}
41
42 virtual const char* what() const noexcept { return message.c_str(); }
43
44 protected:
45 std::string message;
46 };
47
48 class OPENCC_EXPORT FileNotFound : public Exception {
49 public:
50 FileNotFound(const std::string& fileName)
51 : Exception(fileName + " not found or not accessible.") {}
52 };
53
54 class OPENCC_EXPORT FileNotWritable : public Exception {
55 public:
56 FileNotWritable(const std::string& fileName)
57 : Exception(fileName + " not writable.") {}
58 };
59
60 class OPENCC_EXPORT InvalidFormat : public Exception {
61 public:
62 InvalidFormat(const std::string& message)
63 : Exception("Invalid format: " + message) {}
64 };
65
66 class OPENCC_EXPORT InvalidTextDictionary : public InvalidFormat {
67 public:
68 InvalidTextDictionary(const std::string& _message, size_t lineNum)
69 : InvalidFormat("") {
70 std::ostringstream buffer;
71 buffer << "Invalid text dictionary at line " << lineNum << ": " << _message;
72 message = buffer.str();
73 }
74 };
75
76 class OPENCC_EXPORT InvalidUTF8 : public Exception {
77 public:
78 InvalidUTF8(const std::string& _message)
79 : Exception("Invalid UTF8: " + _message) {}
80 };
81
82 class OPENCC_EXPORT ShouldNotBeHere : public Exception {
83 public:
84 ShouldNotBeHere() : Exception("ShouldNotBeHere! This must be a bug.") {}
85 };
86
87 } // namespace opencc
opencc::InvalidTextDictionary
Definition: Exception.hpp:66
opencc::InvalidFormat
Definition: Exception.hpp:60
opencc::Exception
Definition: Exception.hpp:34
opencc::InvalidUTF8
Definition: Exception.hpp:76
opencc::FileNotFound
Definition: Exception.hpp:48
opencc::ShouldNotBeHere
Definition: Exception.hpp:82
opencc::FileNotWritable
Definition: Exception.hpp:54