0

I am thinking of ways to break this simple program I have made. I have a try-catch block around a simple division statement, but I do not know if it is impossible to have an exception thrown in this case.

This is my Values.hpp class that creates an object that has a value and an exception_ptr. If an exception pointer is null for the object, then the object is valid.

#ifndef VALUES_HPP
#define VALUES_HPP

#include <exception>

template <typename T>
class Values
{
public:
    Values(T t);
    Values(std::exception_ptr e);
    bool isValid() const;
    T const& getValue() const;

private:
    T val;
    std::exception_ptr error;
};

    template<typename T>
    Values<T>::Values(T t) : val{ t }, error{nullptr}
    {
    }

    template<typename T>
    Values<T>::Values(std::exception_ptr e)
    {
    }

    template<typename T>
    inline bool Values<T>::isValid() const
    {
        return !error;
    }

    template<typename T>
    inline T const & Values<T>::getValue() const
    {
        if (!error) return val;
        std::rethrow_exception(error);
    }

    template <typename T0, typename T1>
    Values<decltype(std::declval<T0>() / std::declval<T1>())> operator/(Values<T0> const& first, Values<T1> const& second)
    {
        if (!first.isValid()) return first;
        if (!second.isValid()) return second;
        try
        {
            auto answer = fiest.getValue() / second.getValue();
            return answer;
        }
        catch (...) //Is this catch unreachable?
        {
            return std::current_exception();
        }
    }

#endif

Here is my main class that is using the Values objects and the division operator. It is in this class that I want to be able to change something to reach the catch statement.

#include “Values.h”
#include <string>

int main()
{
    Values<int> first(10);
    Values<int> second(10);
    const auto answer = first / second;

    Values<int> third(std::make_exception_ptr(std::string(“test1”)));
    Values<int> fourth(10);
    const auto answer2 = third / fourth;
}

Are there any ways to reach that catch statement? Or is it unreachable. I have not been able to find a way. Additional code added to the main class is allowed.

bmb
  • 361
  • 2
  • 13
  • 3
    TL;DR of the dupe: division by zero does not throw an exception in the C++ code, so there is nothing to catch. You need to test yourself and throw if the divisor is zero. – NathanOliver Feb 05 '20 at 15:42
  • 1
    This design is concerning. It's integrating exceptions as part of normal program logic, meaning that these exceptions aren't intended to be used exceptionally. It defeats most of what makes exceptions interesting like making it difficult to accidentally swallow and hide errors (this design makes it easy) and failing as near as possible to the source of the error to help debugging (unless you have a debugger attached, you have to wait until the final value to see that it contains an error instead). – François Andrieux Feb 05 '20 at 15:47
  • 2
    It's unreachable for `Values`. It would not be unreachable for `Values` where the division operator overload of the class type may throw. – eerorika Feb 05 '20 at 15:48
  • @FrançoisAndrieux I understand your concern but this also looks like a custom implementation of [std::expected](https://github.com/TartanLlama/expected). – Simon Kraemer Feb 05 '20 at 16:00
  • @NathanOliver I don't see a dupe. The code does not test for DIV/0. – Simon Kraemer Feb 05 '20 at 16:02
  • @SimonKraemer Sorry, I don't understand your comment – NathanOliver Feb 05 '20 at 16:03
  • @FrançoisAndrieux The question is clearly stated: Can the catch block be reached in any way regarding the checks that are done before the try-catch-block. Only because the design may be bad the question is still valid and clearly recognizable as such. It's also no duplicate of the currently linked question as there is no indication on DIV/0 anywhere. The best comment so far comes from eerorika imho. – Simon Kraemer Feb 05 '20 at 16:11
  • 1
    @NathanOliver OP did not ask about division by zero but generally if the catch-block is reachable. I don't see how this is a duplicate of the linked question - so I voted for reopen. – Simon Kraemer Feb 05 '20 at 16:13
  • @SimonKraemer It sounds like you think my comment about the design being concerning had something to do with the question being closed. I did not close the question because of my concerns and I did not advocate for it being closed because of it. You can be critical of code in a question without being critical of the question. – François Andrieux Feb 05 '20 at 16:18
  • @FrançoisAndrieux I do not believe my question is a duplicate of the one tagged to the post. However, eerorika imho provided an insightful answer that I believe will help me solve my issue. I think using a class_type that throws when certain division is done will allow for the catch-statement to be reached. – bmb Feb 05 '20 at 16:29
  • This doesn't address the question, but the constructor that takes `std::exception_ptr e` should initialize `error` with `e`. – Pete Becker Feb 05 '20 at 18:05

0 Answers0