| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-2014 Carbo Kuo <[email protected]> |
| 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 |
| 29 | // supported |
| 30 | #define noexcept |
| 31 | #endif // ifdef _MSC_VER |
| 32 | |
| 33 | namespace opencc { |
| 34 | |
| 35 | class OPENCC_EXPORT Exception { |
| 36 | public: |
| 37 | Exception() {} |
| 38 | |
| 39 | virtual ~Exception() throw() {} |
| 40 | |
| 41 | Exception(const std::string& _message) : message(_message) {} |
| 42 | |
| 43 | virtual const char* what() const noexcept { return message.c_str(); } |
| 44 | |
| 45 | protected: |
| 46 | std::string message; |
| 47 | }; |
| 48 | |
| 49 | class OPENCC_EXPORT FileNotFound : public Exception { |
| 50 | public: |
| 51 | FileNotFound(const std::string& fileName) |
| 52 | : Exception(fileName + " not found or not accessible.") {} |
| 53 | }; |
| 54 | |
| 55 | class OPENCC_EXPORT FileNotWritable : public Exception { |
| 56 | public: |
| 57 | FileNotWritable(const std::string& fileName) |
| 58 | : Exception(fileName + " not writable.") {} |
| 59 | }; |
| 60 | |
| 61 | class OPENCC_EXPORT InvalidFormat : public Exception { |
| 62 | public: |
| 63 | InvalidFormat(const std::string& message) |
| 64 | : Exception("Invalid format: " + message) {} |
| 65 | }; |
| 66 | |
| 67 | class OPENCC_EXPORT InvalidTextDictionary : public InvalidFormat { |
| 68 | public: |
| 69 | InvalidTextDictionary(const std::string& _message, size_t lineNum) |
| 70 | : InvalidFormat("") { |
| 71 | std::ostringstream buffer; |
| 72 | buffer << "Invalid text dictionary at line " << lineNum << ": " << _message; |
| 73 | message = buffer.str(); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | class OPENCC_EXPORT InvalidUTF8 : public Exception { |
| 78 | public: |
| 79 | InvalidUTF8(const std::string& _message) |
| 80 | : Exception("Invalid UTF8: " + _message) {} |
| 81 | }; |
| 82 | |
| 83 | class OPENCC_EXPORT ShouldNotBeHere : public Exception { |
| 84 | public: |
| 85 | ShouldNotBeHere() : Exception("ShouldNotBeHere! This must be a bug.") {} |
| 86 | }; |
| 87 | |
| 88 | } // namespace opencc |