xentara-utils v1.2.1
Xentara utilities library
Loading...
Searching...
No Matches
xentara::utils::eh::ExceptionGuard Class Referencefinal

A class that detects whether the stack is currently being wound up. More...

#include <eh/ExceptionGuard.hpp>

Public Member Functions

 ExceptionGuard ()=default
 Default constructor.
 
 ExceptionGuard (const ExceptionGuard &)
 This class is copyable, but the state is not actually copied.
 
auto operator= (const ExceptionGuard &rhs) -> ExceptionGuard
 This class is assigneble, but the state is not actually copied.
 
 operator bool () const
 Checks whether an exception has been thrown since the object has been constructed.
 

Detailed Description

A class that detects whether the stack is currently being wound up.

This class can be used to determine whether the stack frame that thje object has been constructed in is currently being wound up. This is useful to determine whether it is safe to throw an exception from a destructor or not.

Many classes need to perform different steps in the destructor, depending on whether an exception was thrown or not. Consider, for example, a class that implements buffered file writing. In the destructor, the object must perform different cleanup, depending on whether an exception was thrown or not:

  • Normally, the file class will flush the write buffer, and then close the file. If an error occurs in either operation, the class must throw an exception from the destructor, to notify the caller that the file was not correctly written to disk. Otherwise, the file would be silently corrupt.
  • If an exception is thrown anywhere while writing the file, the class will just silently close the file, but not attempt to flush the buffers, and not report any errors closing the file. Reporting errors is not necessary, since the caller will already catch the preexisting exception, and thus already know that the file is unusable.

The class ExceptionGuard can be used like this to distinguish between the two cases:

class MyClass
{
public:
~MyClass() noexcept(false)
{
if(_guard)
{
// No exception has been thrown. You can safely throw exceptions here.
}
else
{
// An exception has been thrown. You should silently ignore errors here.
}
}
private:
ExceptionGuard _guard;
};

Constructor & Destructor Documentation

◆ ExceptionGuard() [1/2]

xentara::utils::eh::ExceptionGuard::ExceptionGuard ( )
default

Default constructor.

◆ ExceptionGuard() [2/2]

xentara::utils::eh::ExceptionGuard::ExceptionGuard ( const ExceptionGuard )

This class is copyable, but the state is not actually copied.

Member Function Documentation

◆ operator bool()

xentara::utils::eh::ExceptionGuard::operator bool ( ) const
explicit

Checks whether an exception has been thrown since the object has been constructed.

Returns
returns false if the stack frame that the object was constructed in is currently being wound up

◆ operator=()

auto xentara::utils::eh::ExceptionGuard::operator= ( const ExceptionGuard rhs) -> ExceptionGuard

This class is assigneble, but the state is not actually copied.