• Register
100 points
7 2
Welcome to Kodlogs, programming questions and answer website.

Type of exceptions that can cause that handler to be entered is described by exception-declaration in a handler. Incomplete type, an abstract class type, or an rvalue reference type shall not be denoted by exception-declaration. And also it shall not denote a pointer or reference to an incomplete type, other than void*, const void*, volatile void*, or const volatile void*.
A handler is a match for an exception object of type E if:

  • The handler is of type cv T or cv T& and E and T are the same type (ignoring the top-level cv-qualifiers), 
  • the handler is of type cv T or cv T& and T is an unambiguous public base class of E, 
  • the handler is of type cv T or const T& where T is a pointer or pointer to member type and E is a pointer or pointer to member type that can be converted to T by one or more of a standard pointer conversion not involving conversions to pointers to private or protected or ambiguous classes
  • a function pointer conversion
  • a qualification conversion, or
  • the handler is of type cv T or const T& where T is a pointer or pointer to member type and E is std​::​nullptr_­t.
     

For more detailed information check out this link.

Cause

If we do not apply any of these points for a case of literals → std::string conversion. Then it will cause the exception being uncaught.
 

Solution

It is best to follow the above bullet points. A dedicated exception type will allow handling the error in a very powerful way.
 
Example
#include <iostream>
#include <iomanip>

using namespace std;

//Template for Maximum

template <class X>
X Maximum(X arg1, X arg2)
{
    if (arg1 > arg2)
        return arg1;
    else
        return arg2;
}

//Template for Minimum

template <class M>
M Minimum(M arg1, M arg2)
{
    if (arg1 > arg2)
        return arg2;
    else
        return arg1;
}

/* Template for Divide(D arg1, D arg2) arg1: the dividend arg2: the divisor Description: 
Divides arg1 by arg2. If arg2 equals zero then an exception is thrown. */

template <class D>
D Divide(D arg1, D arg2)
{
    if (arg2 == 0) {
        throw "You cannot devide by zero! ";
    }
    return (arg1 / arg2);
}

int main()
{

    int a, b;
    float c, d;
    double e, f;
    a = 2;
    b = 22;
    cout << setprecision(4) << fixed << showpoint << "min:" << Minimum(a, b) << "\tmax: " << Maximum(a, b) << endl;
    c = 4.7f;
    d = 2.97f;
    cout << setprecision(4) << fixed << showpoint << "min:" << Minimum(c, d) << "\tmax: " << Maximum(c, d) << endl;

    e = 387.78;
    f = 387.7798;
    cout << setprecision(4) << fixed << showpoint << "min:" << Minimum(e, f) << "\tmax: " << Maximum(e, f) << endl;
    e = 40;
    f = 0;
    try {
        cout << setprecision(4) << fixed << showpoint << "Divide: " << e << '/' << f << " = " << Divide(e, f) << endl;
    }
    catch (string exceptionString) {
        cout << exceptionString;
    }
    system("pause");
    return 0;
}

 

Here the programmer didn’t follow any of these. So either you follow the standard practice or you can
  1. Throw a std::string literal, such that you don’t have to divide it by zero
  2. catch a const char*
Hope this helps.
100 points
7 2