Quick Links:

bal | bbl | bdl | bsl

Namespaces | Typedefs

Component bslstl_stdexceptutil
[Package bslstl]

Provide a utility to throw standard exceptions. More...

Namespaces

namespace  bslstl

Typedefs

typedef bslstl::StdExceptUtil bslstl_StdExceptUtil

Detailed Description

Outline
Purpose:
Provide a utility to throw standard exceptions.
Classes:
bslstl::StdExceptUtil namespace for utilities to throw standard exceptions
Canonical Header:
bsl_stdexcept.h
See also:
stdexcept
Description:
This component provides a means to throw standard exceptions without introducing a compile-time dependency on the standard exception classes. This valuable where header files define function templates or inline functions that may throw these types as exceptions.
Usage:
First we declare a function template that wants to throw a standard exception. Note that the stdexcept header is not included at this point.
  #include <bslstl_stdexceptutil.h>

  template<typename T>
  void testFunction(int selector)
      //  Throw a standard exception according to the specified 'selector'.
  {
    switch(selector) {
      case 1: bslstl::StdExceptUtil::throwRuntimeError("sample message 1");
      case 2: bslstl::StdExceptUtil::throwLogicError("sample message 2");
      default : bslstl::StdExceptUtil::throwInvalidArgument("ERROR");
    }
  }
However, if client code wishes to catch the exception, the .cpp file must #include the appropriate header.
  #include <stdexcept>

  void callTestFunction()
  {
    try {
      testFunction<int>(1);
      assert(0 == "Should throw before reaching here.");
    }
    catch(const runtime_error& ex) {
      assert(0 == std::strcmp(ex.what(), "sample message 1"));
    }

    try {
      testFunction<double>(2);
      assert(0 == "Should throw before reaching here.");
    }
    catch(const logic_error& ex) {
      assert(0 == std::strcmp(ex.what(), "sample message 2"));
    }
  }

Typedef Documentation