Skip to content

Commit 5d82f9b

Browse files
committed
Print more information when a system_error is caught
1 parent 34c77e2 commit 5d82f9b

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/cmdstan/file.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ std::ifstream safe_open(const std::string &fname) {
272272
std::unique_ptr<std::ofstream> safe_create(const std::string &fname,
273273
int sig_figs) {
274274
auto ofs = std::make_unique<std::ofstream>(fname.c_str());
275-
ofs->exceptions(std::ofstream::badbit);
275+
ofs->exceptions(std::ofstream::badbit | std::ofstream::failbit);
276276
if (sig_figs > -1) {
277277
ofs->precision(sig_figs);
278278
}

src/cmdstan/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#include <stdexcept>
2+
#include <iostream>
3+
#include <cerrno>
4+
15
#include <cmdstan/command.hpp>
26
#include <stan/services/error_codes.hpp>
37

@@ -8,6 +12,11 @@ int main(int argc, const char *argv[]) {
812
return cmdstan::return_codes::OK;
913
else
1014
return cmdstan::return_codes::NOT_OK;
15+
} catch (std::system_error &e) {
16+
// system_error is thrown by std::ofstream but the
17+
// message is not always helpful, so we also print errno
18+
std::cerr << e.what() << ": " << std::strerror(errno) << std::endl;
19+
return cmdstan::return_codes::NOT_OK;
1120
} catch (const std::exception &e) {
1221
std::cerr << e.what() << std::endl;
1322
return cmdstan::return_codes::NOT_OK;

0 commit comments

Comments
 (0)