1 /* 2 * Copyright 2011 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // Exceptions used in sfntly 18 19 #ifndef SFNTLY_CPP_SRC_SFNTLY_PORT_EXCEPTION_TYPE_H_ 20 #define SFNTLY_CPP_SRC_SFNTLY_PORT_EXCEPTION_TYPE_H_ 21 22 #if !defined (SFNTLY_NO_EXCEPTION) 23 24 #include <exception> 25 #include <string> 26 #include <sstream> 27 28 namespace sfntly { 29 30 class Exception : public std::exception { 31 public: Exception()32 Exception() : what_("Unknown exception") {} Exception(const char * message)33 explicit Exception(const char* message) throw() { SetMessage(message); } ~Exception()34 virtual ~Exception() throw() {} what()35 virtual const char* what() const throw() { return what_.c_str(); } 36 37 protected: SetMessage(const char * message)38 void SetMessage(const char* message) throw() { 39 try { 40 what_ = message; 41 } catch (...) {} 42 } 43 44 private: 45 std::string what_; 46 }; 47 48 class IndexOutOfBoundException : public Exception { 49 public: IndexOutOfBoundException()50 IndexOutOfBoundException() throw() : Exception("Index out of bound") {} IndexOutOfBoundException(const char * message)51 explicit IndexOutOfBoundException(const char* message) throw() 52 : Exception(message) {} IndexOutOfBoundException(const char * message,int32_t index)53 IndexOutOfBoundException(const char* message, int32_t index) throw() { 54 try { 55 std::ostringstream msg; 56 msg << message; 57 msg << ":"; 58 msg << index; 59 SetMessage(msg.str().c_str()); 60 } catch (...) {} 61 } ~IndexOutOfBoundException()62 virtual ~IndexOutOfBoundException() throw() {} 63 }; 64 65 class IOException : public Exception { 66 public: IOException()67 IOException() throw() : Exception("I/O exception") {} IOException(const char * message)68 explicit IOException(const char* message) throw() : Exception(message) {} ~IOException()69 virtual ~IOException() throw() {} 70 }; 71 72 class ArithmeticException : public Exception { 73 public: ArithmeticException()74 ArithmeticException() throw() : Exception("Arithmetic exception") {} ArithmeticException(const char * message)75 explicit ArithmeticException(const char* message) throw() 76 : Exception(message) {} ~ArithmeticException()77 virtual ~ArithmeticException() throw() {} 78 }; 79 80 class UnsupportedOperationException : public Exception { 81 public: UnsupportedOperationException()82 UnsupportedOperationException() throw() : 83 Exception("Operation not supported") {} UnsupportedOperationException(const char * message)84 explicit UnsupportedOperationException(const char* message) throw() 85 : Exception(message) {} ~UnsupportedOperationException()86 virtual ~UnsupportedOperationException() throw() {} 87 }; 88 89 class RuntimeException : public Exception { 90 public: RuntimeException()91 RuntimeException() throw() : Exception("Runtime exception") {} RuntimeException(const char * message)92 explicit RuntimeException(const char* message) throw() 93 : Exception(message) {} ~RuntimeException()94 virtual ~RuntimeException() throw() {} 95 }; 96 97 class NoSuchElementException : public Exception { 98 public: NoSuchElementException()99 NoSuchElementException() throw() : Exception("No such element") {} NoSuchElementException(const char * message)100 explicit NoSuchElementException(const char* message) throw() 101 : Exception(message) {} ~NoSuchElementException()102 virtual ~NoSuchElementException() throw() {} 103 }; 104 105 class IllegalArgumentException : public Exception { 106 public: IllegalArgumentException()107 IllegalArgumentException() throw() : Exception("Illegal argument") {} IllegalArgumentException(const char * message)108 explicit IllegalArgumentException(const char* message) throw() 109 : Exception(message) {} ~IllegalArgumentException()110 virtual ~IllegalArgumentException() throw() {} 111 }; 112 113 class IllegalStateException : public Exception { 114 public: IllegalStateException()115 IllegalStateException() throw() : Exception("Illegal state") {} IllegalStateException(const char * message)116 explicit IllegalStateException(const char* message) throw() 117 : Exception(message) {} ~IllegalStateException()118 virtual ~IllegalStateException() throw() {} 119 }; 120 121 } // namespace sfntly 122 123 #endif // #if !defined (SFNTLY_NO_EXCEPTION) 124 125 #endif // SFNTLY_CPP_SRC_SFNTLY_PORT_EXCEPTION_TYPE_H_ 126