Catch Multiple Custom Exceptions? - C++

前端 未结 7 1451
我在风中等你
我在风中等你 2020-12-24 10:44

I\'m a student in my first C++ programming class, and I\'m working on a project where we have to create multiple custom exception classes, and then in one of our event handl

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 11:12

    I run into the same problem and here is what I ended up with:

      std::shared_ptr MappedImage::get(const std::string & image_dir,
                                                    const std::string & name,
                                                    const Packet::Checksum & checksum) {
        try {
          return std::shared_ptr(images_.at(checksum));
        } catch (std::out_of_range) {
        } catch (std::bad_weak_ptr) {
        }
        std::shared_ptr img =
          std::make_shared(image_dir, name, checksum);
        images_[checksum_] = img;
        return img;
      }
    

    In my case the function returns when it doesn't get an exception. So I don't actually have to do anything inside the catch but can do the work outside the try.

提交回复
热议问题