-
Notifications
You must be signed in to change notification settings - Fork 9
/
ol-exceptions.h
86 lines (67 loc) · 2.51 KB
/
ol-exceptions.h
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef _OL_EXCEPTIONS_H
#define _OL_EXCEPTIONS_H
#include "hfstol-stdafx.h"
#include <string>
#include <sstream>
#include <cstring>
namespace hfst_ospell
{
// This structure is inherited from for each exception. Taken from HFST library
// code.
//! @brief Top level exception class for ospell related errors.
//! Ospell exceptions can hold basic back-track information for programmer as
//! well as human readable explanation.
struct OspellException
{
std::string name; //!< short description of exception
std::string file; //!< file name of exception
size_t line; //!< line number of exception
OspellException(void) {}
//!
//! construct exception with name, file and location
OspellException(const std::string &name,const std::string &file,size_t line):
name(name),
file(file),
line(line)
{}
//!
//! create string representation of exception for output
std::string operator() (void) const
{
std::ostringstream o;
o << "Exception: "<< name << " in file: "
<< file << " on line: " << line;
return o.str();
}
//!
//! create char array representation of exception for output
const char* what()
{
std::ostringstream o;
o << file << ":" << line << ":" << name;
return strdup(o.str().c_str());
}
};
// These macros are used instead of the normal exception facilities.
#define HFSTOSPELL_THROW(E) throw E(#E,__FILE__,__LINE__)
#define HFSTOSPELL_THROW_MESSAGE(E,M) throw E(std::string(#E)+": "+std::string(M)\
,__FILE__,__LINE__)
#define HFSTOSPELL_EXCEPTION_CHILD_DECLARATION(CHILD) \
struct CHILD : public OspellException \
{ CHILD(const std::string &name,const std::string &file,size_t line):\
OspellException(name,file,line) {}}
#define HFST_CATCH(E) \
catch (const E &e) \
{ \
std::cerr << e.file << ", line " << e.line << ": " << \
e() << std::endl; \
}
// Now the exceptions themselves
HFSTOSPELL_EXCEPTION_CHILD_DECLARATION(HeaderParsingException);
HFSTOSPELL_EXCEPTION_CHILD_DECLARATION(AlphabetParsingException);
HFSTOSPELL_EXCEPTION_CHILD_DECLARATION(IndexTableReadingException);
HFSTOSPELL_EXCEPTION_CHILD_DECLARATION(TransitionTableReadingException);
HFSTOSPELL_EXCEPTION_CHILD_DECLARATION(UnweightedSpellerException);
HFSTOSPELL_EXCEPTION_CHILD_DECLARATION(TransducerTypeException);
} // namespace
#endif // _OL_EXCEPTIONS_H