C++Guns – RoboBlog blogging the bot

17.09.2022

C++ Guns: throw and catch all standard exceptions for fun

Filed under: Allgemein — Tags: — Thomas @ 19:09

This example throw and catch all standard exceptions just for fun
exception list from https://en.cppreference.com/w/cpp/error/exception
sorted after C++ Standard

searching a stack trace? look at https://en.cppreference.com/w/cpp/utility/basic_stacktrace

/* This example throw and catch all standard exceptions just for fun
 * exception list from https://en.cppreference.com/w/cpp/error/exception
 * sorted after C++ Standard
 *
 * searching a stack trace? look at https://en.cppreference.com/w/cpp/utility/basic_stacktrace
 */

#include <iostream>
#include <exception>
#include <future>
#include <regex>
#include <filesystem>
#include <chrono>
#include <any>
#include <optional>
#include <variant>
//#include <format> // C++20. Not implemented in GCC 12
// #include <expected> // C++23


int nCatched = 0;

template<typename T>
void check(const T& ex, std::string_view exceptionType) {
  nCatched++;
  std::string_view what(ex.what());
  // only compare the first chars from what() message, we are not intereseting in stuff after the exception type
  if(what.substr(0,exceptionType.size()) != exceptionType) {
      std::cerr << exceptionType << " what(): " << ex.what() << "\n";
      std::cerr << "\nERROR: Not all exception derived from " << exceptionType << " catched...\n";
      ::exit(EXIT_FAILURE);
    }
}

int main() {

  bool finish = false;
  int nThrow = 0;
  while(not finish){
    try {
      switch(nThrow) {
        case  1: ++nThrow; throw std::exception();
        case  2: ++nThrow; throw std::logic_error("std::logic_error");
        case  3: ++nThrow; throw std::invalid_argument("std::invalid_argument");
        case  4: ++nThrow; throw std::domain_error("std::domain_error");
        case  5: ++nThrow; throw std::length_error("std::length_error");
        case  6: ++nThrow; throw std::out_of_range("std::out_of_range");
        case  7: ++nThrow; throw std::runtime_error("std::runtime_error");
        case  8: ++nThrow; throw std::range_error("std::range_error");
        case  9: ++nThrow; throw std::overflow_error("std::overflow_error");
        case 10: ++nThrow; throw std::underflow_error("std::underflow_error");
        case 11: ++nThrow; throw std::bad_typeid();
        case 12: ++nThrow; throw std::bad_alloc();
        case 13: ++nThrow; throw std::bad_exception();
        case 14: ++nThrow; throw std::regex_error(std::regex_constants::error_collate); // C++11
        case 15: ++nThrow; throw std::system_error(ENOENT, std::system_category(), "std::system_error"); // C++11
        case 16: ++nThrow; throw std::ios_base::failure("std::ios_base::failure"); // C++11
        case 17: ++nThrow; throw std::future_error(std::future_errc::broken_promise); // C++11
        case 18: ++nThrow; throw std::bad_weak_ptr(); // C++11
        case 19: ++nThrow; throw std::bad_function_call(); // C++11
        case 20: ++nThrow; throw std::bad_array_new_length(); // C++11
        case 21: ++nThrow; throw std::filesystem::filesystem_error("std::filesystem::filesystem_error", std::error_code(ENOENT, std::system_category())); // C++17
        case 22: ++nThrow; throw std::bad_any_cast(); // C++17
        case 23: ++nThrow; throw std::bad_optional_access(); // C++17
        case 24: ++nThrow; throw std::bad_variant_access(); // C++17
        // case 25: throw std::chrono::nonexistent_local_time(); // C++ 20. Not implemented in GCC 12
        // case 26: throw std::chrono::ambiguous_local_time(); // C++20. Not implemented in GCC 12
        // case 27: throw std::format_error(); // C++20. Not implemented in GCC 12
        // case 28: throw std::bad_expected_access(); // C++23
        // case 29: throw std::tx_exception(); TODO
        default: {
          finish = true;
        }
      }
    }
    catch(std::bad_variant_access& ex) {
      check(ex, "bad variant access");
    }
    catch(std::bad_exception& ex) {
      check(ex, "std::bad_exception");
    }
    catch(std::bad_array_new_length& ex) {
      check(ex, "std::bad_array_new_length");
    }
    catch(std::bad_alloc& ex) {
      check(ex, "std::bad_alloc");
    }
    catch(std::bad_function_call& ex) {
      check(ex, "bad_function_call");
    }
    catch(std::bad_weak_ptr& ex) {
      check(ex, "bad_weak_ptr");
    }
    catch(std::bad_optional_access& ex) {
      check(ex, "bad optional access");
    }
    catch(std::bad_any_cast& ex) {
      check(ex, "bad any_cast");
    }
    catch(std::bad_typeid& ex) {
      check(ex, "std::bad_typeid");
    }
    catch(std::filesystem::filesystem_error& ex) {
      check(ex, "filesystem error");
    }
    catch(std::ios_base::failure& ex) {
      check(ex, "std::ios_base::failure");
    }
    catch(std::system_error& ex) {
      check(ex, "std::system_error");
    }
    catch(std::regex_error& ex) {
      check(ex, "Invalid collating element in regular expression"); // regex_error.what does not print the exception type first...
    }
    catch(std::underflow_error& ex) {
      check(ex, "std::underflow_error");
    }
    catch(std::overflow_error& ex) {
      check(ex, "std::overflow_error");
    }
    catch(std::range_error& ex) {
      check(ex, "std::range_error");
    }
    catch(std::runtime_error& ex) {
      check(ex, "std::runtime_error");
    }
    catch(std::future_error& ex) {
      check(ex, "std::future_error");
    }
    catch(std::out_of_range& ex) {
      check(ex, "std::out_of_range");
    }
    catch(std::length_error& ex) {
      check(ex, "std::length_error");
    }
    catch(std::domain_error& ex) {
      check(ex, "std::domain_error");
    }
    catch(std::invalid_argument& ex) {
      check(ex, "std::invalid_argument");
    }
    catch(std::logic_error& ex) {
      check(ex, "std::logic_error");
    }
    catch(std::exception& ex) {
      check(ex, "std::exception");
    }
  } // while


  if(nThrow != nCatched) {
    std::cerr << nThrow << " exception thrown but " << nCatched << " catched\n";
  } else {
    std::cout << "All exceptions which was thrown was catched\n";
  }

  return EXIT_SUCCESS;
}

$ ./a.out
All exceptions which was thrown was catched

07.09.2022

C++ Guns: Streams display the format flags

Filed under: Allgemein — Tags: — Thomas @ 12:09

Display which format flags are currently set e.g. fixed, scientific, dec, hex

std::ostream& operator<<(std::ostream& s, const std::ios::fmtflags f) {
   if(f & std::ios::boolalpha) s << "boolalpha ";
   if(f & std::ios::dec) s << "dec ";
   if(f & std::ios::hex) s << "hex ";
   if(f & std::ios::oct) s << "oct ";
   if(f & std::ios::fixed) s << "fixed ";
   if(f & std::ios::scientific) s << "scientific ";
   if(f & std::ios::right) s << "right ";
   if(f & std::ios::left) s << "left ";
   if(f & std::ios::internal) s << "internal ";
   if(f & std::ios::showbase) s << "showbase ";
   if(f & std::ios::showpoint) s << "showpoint ";
   if(f & std::ios::showpos) s << "showpos ";
   if(f & std::ios::uppercase) s << "uppercase ";
   if(f & std::ios::unitbuf) s << "unitbuf ";
   if(f & std::ios::skipws) s << "skipws ";
   return s;
}

std::cout << std::cout.flags() << "\n";

Example output

dec fixed skipws 

https://en.cppreference.com/w/cpp/io/ios_base/flags

Powered by WordPress