Skip to content

ACtE0306 Generic programming and exception handling

Function and class templates

Templates define algorithms or types parameterized by types or values.

Function template example:

template <class T>
T max_of(T a, T b) {
    return (a < b) ? b : a;
}

Class template example:

template <class T>
class Box {
    T value;
public:
    explicit Box(T v) : value(v) {}
    T get() const;
};

template <class T>
T Box<T>::get() const {
    return value;
}

Recognition points:

  • compiler generates concrete instantiations as needed;
  • template overloading is possible if signatures differ meaningfully;
  • template definitions generally need to be visible at the point of use unless separate mechanisms are used.

STL basics: containers, algorithms, iterators

Category Examples Recognition
Sequence containers vector, deque, list ordered element collections
Associative containers set, map, multiset, multimap key-based lookup
Unordered containers unordered_set, unordered_map hash-based average lookup
Adapters stack, queue, priority_queue restricted interfaces
Algorithms sort, find, count, copy generic operations over iterator ranges
Iterators input/output/forward/bidirectional/random-access generalized traversal objects

Recognition traps:

  • vector provides contiguous storage;
  • list provides bidirectional links, not random-access iterators;
  • not every algorithm works with every iterator category.

Exception handling constructs

Basic flow:

try {
    // code that may throw
} catch (const std::exception& e) {
    // handler
}

Core meaning:

  • throw signals an exceptional condition;
  • try marks protected code;
  • catch handles matching exceptions.
  • exception handling permits controlled propagation, cleanup, reporting, or recovery from runtime failures; it does not guarantee that every failure can be recovered from.

Multiple handlers are tested in order. More specific handlers should appear before more general ones.

Multiple handling, rethrow, and catch-all

Feature Syntax / cue
Multiple handlers several catch blocks
Rethrow current exception throw;
Catch all catch (...)

Critical distinction:

  • throw; rethrows the currently handled exception unchanged;
  • throw e; throws a copy of e and may slice if e is a base object.

Exceptions with arguments and standard guidance

User-defined exception objects can carry data such as messages or error codes.

Best-practice recognition cues:

  • throw by value;
  • catch by reference, usually const reference;
  • derive custom exceptions from a standard exception family when meaningful.

Exception specifications and uncaught exceptions

The syllabus names exception specification for function and handling uncaught/unexpected exceptions. Language-standard accuracy matters here.

Topic Standard-accurate note
Dynamic exception specification like throw(int) old C++ feature, deprecated and removed in modern C++
noexcept modern way to state non-throwing intent/contract
Uncaught exception if an exception escapes with no matching handler, std::terminate is ultimately invoked
unexpected violating a pre-C++17 dynamic exception specification invoked std::unexpected; this mechanism was removed in C++17
noexcept violation an exception escaping a non-throwing noexcept function invokes std::terminate

So exam stems based on older C++ textbooks may mention unexpected() and old exception specifications; modern standard practice prefers noexcept and standard termination rules.

C setjmp/longjmp caution

These are not C++ exception mechanisms.

  • setjmp saves an execution environment.
  • longjmp jumps back to a previously saved environment.
  • They do not provide typed stack unwinding semantics like C++ exceptions.

This distinction matters because model-key lore sometimes swaps their roles.

Generic-exception revision box

  • Templates parameterize code by type or value.
  • STL = containers + algorithms + iterators.
  • vector is contiguous; list is linked and not random-access.
  • throw signals; try protects; catch handles.
  • throw; rethrows the current exception.
  • Old dynamic exception specifications are obsolete; noexcept is the modern standard mechanism.