Skip to content

Commit 9e15c41

Browse files
committed
Reading and Writing now throw std::domain_exception is they fail to open the file, allowing exception handling by the library user instead of trying to define the behavior in sac-format (previously used std::cerr to output to console).
1 parent 3f4bde4 commit 9e15c41

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

src/sac_format.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,7 @@ void Trace::data2(const std::vector<double> &input) {
775775
Trace::Trace(const std::filesystem::path &path) {
776776
std::ifstream file(path, std::ifstream::binary);
777777
if (!file) {
778-
std::cerr << path.string() << " could not be read.\n";
779-
return;
778+
throw std::domain_error(path.string() + "cannot be opened to read.");
780779
}
781780
file.seekg(0);
782781
//--------------------------------------------------------------------------
@@ -969,8 +968,7 @@ Trace::Trace(const std::filesystem::path &path) {
969968
void Trace::write(const std::filesystem::path &path, const bool legacy) const {
970969
std::ofstream file(path, std::ios::binary | std::ios::out | std::ios::trunc);
971970
if (!file) {
972-
std::cerr << path.string() << "cannot be written.\n";
973-
return;
971+
throw std::domain_error(path.string() + "cannot be opened to write.");
974972
}
975973
const int header_version{legacy ? old_hdr_version : modern_hdr_version};
976974
write_words(&file, convert_to_word(static_cast<float>(delta())));

src/sac_format.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <iostream>
1919
#include <limits>
2020
#include <numbers>
21+
// std::domain_error
22+
#include <stdexcept>
2123
#include <string>
2224
#include <string_view>
2325
#include <unordered_map>

0 commit comments

Comments
 (0)