Learn C++ Online

Handling non-local errors at runtime (part 2)

On this page
This lesson contains approximately 13 minutes of video content.

Types of exceptions defined for us in <stdexcept>

Preconditions / postconditions

In this problem, you will implement the function FindPersonById(const std::vector<Person>& people, unsigned int id). This function will search people for a Person with id. If the person exists, simply return people.at(loc) where loc is the index of the Person in people. If a Person with id is not found in people, report this to the caller by throwing an std::runtime_error with the message Invalid id number.
#include <iostream> #include <string> #include <vector> #include "person.hpp" #include "utilities.hpp" int main() { }
#ifndef PERSON_HPP #define PERSON_HPP #include <string> struct Person { std::string first_name; std::string last_name; unsigned int id; }; #endif
#ifndef UTILITIES_HPP #define UTILITIES_HPP #include <vector> #include "person.hpp" const Person& FindPersonById(const std::vector<Person>& people, unsigned int id); #endif
#include <stdexcept> #include "person.hpp" #include "utilities.hpp"
> Click Run to compile and run your code.