Skip to content

Commit 438bf2c

Browse files
committed
Adjust exception behavior
1 parent de9aa59 commit 438bf2c

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/cxx_supportlib/IOTools/IOUtils.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// https://bugzilla.redhat.com/show_bug.cgi?id=165427
3030
// Also needed for SO_PEERCRED.
3131
#define _GNU_SOURCE
32+
#include <exception>
3233
#endif
3334

3435
#include <oxt/system_calls.hpp>
@@ -672,13 +673,23 @@ FdGuard::FdGuard(int fd, const char *sourceFile, unsigned int sourceLine)
672673
}
673674
}
674675

675-
FdGuard::~FdGuard() {
676+
FdGuard::~FdGuard() noexcept(false) {
676677
if (mFd != -1) {
677678
try {
678679
safelyClose(mFd);
679680
} catch (const std::exception &e) {
680-
P_WARN("Error closing file descriptor " << mFd << ": " << e.what());
681-
return;
681+
bool uncaughtException =
682+
#if __cplusplus >= 201703L
683+
std::uncaught_exceptions() > 0;
684+
#else
685+
std::uncaught_exception();
686+
#endif
687+
if (uncaughtException) {
688+
P_WARN("Error closing file descriptor " << mFd << ": " << e.what());
689+
return;
690+
} else {
691+
throw e;
692+
}
682693
}
683694
P_LOG_FILE_DESCRIPTOR_CLOSE(mFd);
684695
}
@@ -698,12 +709,12 @@ FdGuard::operator=(FdGuard &&other) {
698709
}
699710

700711
void
701-
FdGuard::clear() {
712+
FdGuard::clear() noexcept {
702713
mFd = -1;
703714
}
704715

705716
void
706-
FdGuard::runNow() {
717+
FdGuard::runNow() noexcept(false) {
707718
if (mFd != -1) {
708719
safelyClose(mFd);
709720
P_LOG_FILE_DESCRIPTOR_CLOSE(mFd);

src/cxx_supportlib/IOTools/IOUtils.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,16 +370,21 @@ class FdGuard {
370370
FdGuard(const FdGuard &other) = delete;
371371
FdGuard(FdGuard &&other);
372372
FdGuard(int fd, const char *sourceFile, unsigned int sourceLine);
373-
~FdGuard();
373+
/** @throws SystemException File descriptor close error. If exception is already being thrown, then only warns. */
374+
~FdGuard() noexcept(false);
374375

375376
FdGuard &operator=(const FdGuard &other) = delete;
376377
FdGuard &operator=(FdGuard &&other);
377378

378379
/** Don't close file descriptor at object destruction. */
379-
void clear();
380+
void clear() noexcept;
380381

381-
/** Close file descriptor now. Idempotent. */
382-
void runNow();
382+
/**
383+
* Close file descriptor now. Idempotent.
384+
*
385+
* @throws SystemException
386+
*/
387+
void runNow() noexcept(false);
383388
};
384389

385390

0 commit comments

Comments
 (0)