C ++ Throwing the same error message from different places in the code

I want to skip the same error from different places. Here is an example:

int devide(int a,int b)
{
  if(b==0) {
    throw "b must be different from 0";
  }

  return 0;
}

int mod(int a,int b)
{
  if(b==0) {
    throw "b must be different than 0";
  }

  return 0;
}

And in the main function:

int main()
{
  try {
    devide(1,0);
  } catch(char const* e) {
    cout << e << endl;
  }
  try {
    mod(1,0);
  } catch(char const* e) {
    cout << e << endl;
  }

  return 0;
}

Now the program output:

b must be different from 0
b must be different than 0

As you can see, the error is the same in nature, but the message is different. So what is the best practice that I can follow in this case so that it can be scalable? Let's say that I have 100 different types of exceptions, but when I throw an exception for devisor to be different from 0, I want to get the same messege wherever the exception is thrown.

EDIT: I was thinking about two options:

  • For each exception that I can throw, I will define my own class that inherits std::exception. Each exception can be added to Exceptions.cpp.
  • class MyException, std::exception, . - ( , ), , . , MyException, :

    int devide(int a,int b)
    {
      if(b==0) {
        throw new MyException(MyException::DEVISION_BY_ZERO);
      }
    }
    

MyException::DEVISION_BY_ZERO .

, , .

, aproach, ?

+4
3

, , .

class Exception : public std::exception
{
    public:                
        template< typename... Args >
        Exception( const std::string& msg, Args... args );

        virtual ~Exception() throw ();

        //! Overrides std::exception
        virtual const char* what() const throw();

    private:
        //! Log the message_
        void log() const;

    protected:
        std::string message_;
};

template< typename... Args >
Exception::Exception( const std::string& msg, Args... args )
    : message_( StrFmt::format( msg, args ... ) )
{
    log();
}

class MyException : public Exception
{
    MyException() : Exception( std::string("b must be different from 0") )
}

, , .

static void terminate()
{
    static bool tried_rethrow = false;
    try
    {
        if ( !tried_rethrow )
        {
            tried_rethrow = true;
            throw;
        }
    }
    catch ( const std::runtime_error& err )
    {
        std::cout << err.what();
    }
    catch ( const Exception& err )
    {
        std::cout << err.what();
    }
    catch ( ... )
    {
    }
} 

main.cpp:

std::set_terminate( terminate );

, .

+3

. std::runtime_error, .

catch(), :

class DivByZeroError : public std::runtime_error {
public:
    DivByZeroError() : std::runtime_error("divisor must be different from 0") {}
    // Alternate constructor to provide a specific error message
    DivByZeroError(const std::string& s) : std::runtime_error(s) {}
}

class ModWithZeroError : public std::runtime_error {
public:
    ModWithZeroError () : std::runtime_error("divisor must be different than 0") {}
    ModWithZeroError (const std::string& s) : std::runtime_error(s) {}
}

int main()
{
  try {
    devide(1,0);
    mod(1,0);
  } catch(const DivByZeroError& e) {
    cout << e.what() << endl;
  } catch(const ModWithZeroError & e) {
    cout << e.what() << endl;
  } catch(const std::exception & e) { // Any other exceptions
    cout << e.what() << endl;
  }

  return 0;
}

@CaptainOblivious, std::invalid_argument, , - , .

+3

, , , , std::exception... .

, "" :

, 100 , - , devisor 0 messege , .

, , , .

:

class DivByZeroError : public std::runtime_error {
public:
    DivByZeroError() : std::runtime_error("divisor must be different from 0") {}
    // Alternate constructor to provide a specific error message
    DivByZeroError(const std::string& s) : std::runtime_error(s) {}
}

. 100% . a DivByZeroError, () .

, , , , , , .

, , . ctor, what() std::exception:

:

DivByZeroError(std::string const& function, std::string const& file, size_t line, optional<int> dividendValue = none, std::string const& message = "");

: https://softwareengineering.stackexchange.com/questions/278949/why-do-many-exception-messages-not-contain-useful-details

+1

Source: https://habr.com/ru/post/1655482/


All Articles