Skip to content

Error handling

Raul edited this page Apr 29, 2021 · 1 revision

Almost everywhere a piece of uammd code can fail a C++ exception[1] will be thrown that the user can catch and handle.
If the error that resulted in the exception being thrown has not left the program in an unrecoverable state (which is often the case with CUDA errors) the user can handle the exception and continue the execution.
See the following example

#include<uammd.cuh>
#include<utils/exception.h>
#include<cstdlib>
#include<iostream>

using namespace uammd;

auto createSystem(int argc, char **argv){
  try{
    return std::make_shared<System>(argc, argv);
  }
  catch(uammd::exception &e){
    System::log<System::WARNING>("Recovering from System exception");
    System::log<System::ERROR>("Backtrace:");
    uammd::backtrace_nested_exception(e);
    return createSystem(0, nullptr);
  }
}

//Try to call this program with an invalid input, for example ./a.out --device -100
//System creation will fail and try to recover
int main(int argc, char **argv){

  auto sys = createSystem(argc, argv);
  int N =100;
  auto pd = std::make_shared<ParticleData>(N, sys);
  auto pos = pd->getPos(access::location::cpu, access::mode::write);

  try{
    auto pos_illegal = pd->getPos(access::location::cpu, access::mode::read);
  }
  catch(uammd::illegal_property_access &e){
    sys->log<System::WARNING>("Recovering from access exception");
    uammd::backtrace_nested_exception(e);
  }
  sys->finish();
  return EXIT_SUCCESS;
};

References

[1] https://www.cplusplus.com/doc/tutorial/exceptions/









Clone this wiki locally