Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-19 09:22:42

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file geocel/ScopedGeantExceptionHandler.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <exception>
0010 #include <functional>
0011 #include <memory>
0012 
0013 #include "corecel/Config.hh"
0014 
0015 #include "corecel/Assert.hh"
0016 #include "corecel/Macros.hh"
0017 
0018 class G4VExceptionHandler;
0019 
0020 namespace celeritas
0021 {
0022 //---------------------------------------------------------------------------//
0023 /*!
0024  * Convert Geant4 exceptions to RuntimeError during this class lifetime.
0025  *
0026  * Note that creating a \c G4RunManagerKernel resets the exception
0027  * handler, so errors thrown during setup \em cannot be caught by Celeritas,
0028  * and this class can only be used after creating the \c G4RunManager.
0029  *
0030  * Because the underlying Geant4 error handler is thread-local, this class must
0031  * be scoped to inside each worker thread. Additionally, since throwing from a
0032  * worker thread terminates the program, an error handler \em must be specified
0033  * if used in a worker thread: you should probably use a \c
0034  * celeritas::MultiExceptionHandler .
0035  */
0036 class ScopedGeantExceptionHandler
0037 {
0038   public:
0039     //!@{
0040     //! \name Type aliases
0041     using StdExceptionHandler = std::function<void(std::exception_ptr)>;
0042     //!@}
0043 
0044   public:
0045     // Construct with an exception handling function
0046     explicit ScopedGeantExceptionHandler(StdExceptionHandler handle);
0047 
0048     //! Construct, throwing on G4Exception calls
0049     ScopedGeantExceptionHandler() : ScopedGeantExceptionHandler{nullptr} {}
0050 
0051     // Clear on destruction
0052     ~ScopedGeantExceptionHandler();
0053 
0054     //! Prevent copying and moving for RAII class
0055     CELER_DELETE_COPY_MOVE(ScopedGeantExceptionHandler);
0056 
0057   private:
0058 #if CELERITAS_USE_GEANT4
0059     G4VExceptionHandler* previous_{nullptr};
0060     std::unique_ptr<G4VExceptionHandler> current_;
0061 #endif
0062 };
0063 
0064 #if !CELERITAS_USE_GEANT4
0065 //!@{
0066 //! Do nothing if Geant4 is disabled (source file will not be compiled)
0067 inline ScopedGeantExceptionHandler::ScopedGeantExceptionHandler(
0068     StdExceptionHandler)
0069 {
0070     CELER_NOT_CONFIGURED("Geant4");
0071 }
0072 inline ScopedGeantExceptionHandler::~ScopedGeantExceptionHandler() {}
0073 //!@}
0074 #endif
0075 
0076 //---------------------------------------------------------------------------//
0077 }  // namespace celeritas