Skip to content

Commit 34d9f22

Browse files
Wrap exceptions in std::exception to verbose output on exception. (#1316)
1 parent d3e8093 commit 34d9f22

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

llama-util.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <string>
1616
#include <vector>
17+
#include <stdexcept>
1718

1819
#ifdef __has_include
1920
#if __has_include(<unistd.h>)
@@ -74,7 +75,7 @@ struct llama_file {
7475
llama_file(const char * fname, const char * mode) {
7576
fp = std::fopen(fname, mode);
7677
if (fp == NULL) {
77-
throw format("failed to open %s: %s", fname, std::strerror(errno));
78+
throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
7879
}
7980
seek(0, SEEK_END);
8081
size = tell();
@@ -107,10 +108,10 @@ struct llama_file {
107108
errno = 0;
108109
std::size_t ret = std::fread(ptr, size, 1, fp);
109110
if (ferror(fp)) {
110-
throw format("read error: %s", strerror(errno));
111+
throw std::runtime_error(format("read error: %s", strerror(errno)));
111112
}
112113
if (ret != 1) {
113-
throw std::string("unexpectedly reached end of file");
114+
throw std::runtime_error(std::string("unexpectedly reached end of file"));
114115
}
115116
}
116117

@@ -133,7 +134,7 @@ struct llama_file {
133134
errno = 0;
134135
size_t ret = std::fwrite(ptr, size, 1, fp);
135136
if (ret != 1) {
136-
throw format("write error: %s", strerror(errno));
137+
throw std::runtime_error(format("write error: %s", strerror(errno)));
137138
}
138139
}
139140

@@ -180,7 +181,7 @@ struct llama_mmap {
180181
#endif
181182
addr = mmap(NULL, file->size, PROT_READ, flags, fd, 0);
182183
if (addr == MAP_FAILED) {
183-
throw format("mmap failed: %s", strerror(errno));
184+
throw std::runtime_error(format("mmap failed: %s", strerror(errno)));
184185
}
185186

186187
if (prefetch) {
@@ -207,15 +208,15 @@ struct llama_mmap {
207208
DWORD error = GetLastError();
208209

209210
if (hMapping == NULL) {
210-
throw format("CreateFileMappingA failed: %s", llama_format_win_err(error).c_str());
211+
throw std::runtime_error(format("CreateFileMappingA failed: %s", llama_format_win_err(error).c_str()));
211212
}
212213

213214
addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
214215
error = GetLastError();
215216
CloseHandle(hMapping);
216217

217218
if (addr == NULL) {
218-
throw format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str());
219+
throw std::runtime_error(format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str()));
219220
}
220221

221222
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
@@ -245,7 +246,7 @@ struct llama_mmap {
245246

246247
llama_mmap(struct llama_file *, bool prefetch = true) {
247248
(void)prefetch;
248-
throw std::string("mmap not supported");
249+
throw std::runtime_error(std::string("mmap not supported"));
249250
}
250251
#endif
251252
};

0 commit comments

Comments
 (0)