1 /////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
5 /////////////////////////////////////////////////////////////////////////////
6 
7 #ifndef LUCENEEXCEPTION_H
8 #define LUCENEEXCEPTION_H
9 
10 #include "Lucene.h"
11 
12 namespace Lucene {
13 
14 /// Lucene exception container.
15 class LPPAPI LuceneException : public std::exception {
16 public:
17     enum ExceptionType {
18         Null,
19         AlreadyClosed,
20         Compression,
21         CorruptIndex,
22         FieldReader,
23         FileNotFound,
24         IllegalArgument,
25         IllegalState,
26         IndexOutOfBounds,
27         IO,
28         LockObtainFailed,
29         LockReleaseFailed,
30         Lookahead,
31         MergeAborted,
32         Merge,
33         NoSuchDirectory,
34         NullPointer,
35         NumberFormat,
36         OutOfMemory,
37         Parse,
38         QueryParser,
39         Runtime,
40         StaleReader,
41         StopFillCache,
42         Temporary,
43         TimeExceeded,
44         TooManyClauses,
45         UnsupportedOperation
46     };
47 
48     LuceneException(const String& error = EmptyString, LuceneException::ExceptionType type = Null) throw();
49     ~LuceneException() throw();
50 
51 protected:
52     ExceptionType type;
53     String error;
54 
55     std::string _what;
56 
57 public:
58     ExceptionType getType() const;
59     String getError() const;
60     bool isNull() const;
61     void throwException();
62 
63     virtual const char* what() const throw();
64 };
65 
66 template <class ParentException, LuceneException::ExceptionType Type>
67 class ExceptionTemplate : public ParentException {
68 public:
ParentException(error,type)69     ExceptionTemplate(const String& error = EmptyString, LuceneException::ExceptionType type = Type) : ParentException(error, type) {
70     }
71 };
72 
73 typedef ExceptionTemplate<LuceneException, LuceneException::Runtime> RuntimeException;
74 typedef ExceptionTemplate<LuceneException, LuceneException::OutOfMemory> OutOfMemoryError;
75 typedef ExceptionTemplate<LuceneException, LuceneException::Temporary> TemporaryException;
76 typedef ExceptionTemplate<RuntimeException, LuceneException::IllegalState> IllegalStateException;
77 typedef ExceptionTemplate<RuntimeException, LuceneException::IllegalArgument> IllegalArgumentException;
78 typedef ExceptionTemplate<RuntimeException, LuceneException::IndexOutOfBounds> IndexOutOfBoundsException;
79 typedef ExceptionTemplate<RuntimeException, LuceneException::NullPointer> NullPointerException;
80 typedef ExceptionTemplate<RuntimeException, LuceneException::FieldReader> FieldReaderException;
81 typedef ExceptionTemplate<RuntimeException, LuceneException::Merge> MergeException;
82 typedef ExceptionTemplate<RuntimeException, LuceneException::StopFillCache> StopFillCacheException;
83 typedef ExceptionTemplate<RuntimeException, LuceneException::TimeExceeded> TimeExceededException;
84 typedef ExceptionTemplate<RuntimeException, LuceneException::TooManyClauses> TooManyClausesException;
85 typedef ExceptionTemplate<RuntimeException, LuceneException::UnsupportedOperation> UnsupportedOperationException;
86 typedef ExceptionTemplate<IllegalArgumentException, LuceneException::NumberFormat> NumberFormatException;
87 typedef ExceptionTemplate<IllegalStateException, LuceneException::AlreadyClosed> AlreadyClosedException;
88 typedef ExceptionTemplate<LuceneException, LuceneException::IO> IOException;
89 typedef ExceptionTemplate<IOException, LuceneException::CorruptIndex> CorruptIndexException;
90 typedef ExceptionTemplate<IOException, LuceneException::FileNotFound> FileNotFoundException;
91 typedef ExceptionTemplate<IOException, LuceneException::LockObtainFailed> LockObtainFailedException;
92 typedef ExceptionTemplate<IOException, LuceneException::LockReleaseFailed> LockReleaseFailedException;
93 typedef ExceptionTemplate<IOException, LuceneException::MergeAborted> MergeAbortedException;
94 typedef ExceptionTemplate<IOException, LuceneException::StaleReader> StaleReaderException;
95 typedef ExceptionTemplate<FileNotFoundException, LuceneException::NoSuchDirectory> NoSuchDirectoryException;
96 typedef ExceptionTemplate<LuceneException, LuceneException::Lookahead> LookaheadSuccess;
97 typedef ExceptionTemplate<LuceneException, LuceneException::Parse> ParseException;
98 typedef ExceptionTemplate<LuceneException, LuceneException::QueryParser> QueryParserError;
99 typedef ExceptionTemplate<LuceneException, LuceneException::Compression> CompressionException;
100 }
101 
102 #endif
103