Exception handling in C ++ Terminate, called after throwing an instance of 'char const *'

Error: terminating call after invoking instance of 'char const *'

The annex

terminate requires Runtime to terminate in an unusual way. Contact Support.

I'm not sure what causes the compiler to crash when I do this. Any ideas? A little new to programming.

#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; } 
+5
source share
1 answer

The string literal is not std::string . You throw the first, but try to catch the last. Although a std::string can be constructed from a string literal, this will not happen in the catch clause. Conversions that are allowed in the catch clause are described in detail in [except.handle] / 3 :

A handler is a match for an exception object of type E if:

  • A handler is of type cv T or cv T & and E and T are of the same type (ignoring top-level cv qualifiers) or
  • the handler is of type cv T or cv T & and T is a unique public base class E, or
  • the handler is of type cv T or const T & where T is a pointer or pointer to a member type, and E is a pointer or pointer to a member type that can be converted to T by one or more of
    • standard pointer conversion that does not include pointer conversion to private or protected or ambiguous classes
    • function pointer conversion
    • qualification transformation, or
  • the handler is of type cv T or const T & where T is a pointer or a pointer to a member type, and E is std :: nullptr_t.

And not one of them is used to convert literal β†’ std::string .

This ultimately leads to the fact that this exception is unmapped and a call to the std::terminate runtime, which should happen with uncaught exceptions.

In general, it is best to use a highlighted exception type (which may be part of the hierarchy), so the type name itself reports an error. This will allow you to handle the error in more reliable ways if you need to write a handler (or a set of handlers).

If you do not want to follow standard practice for any reason, you should make the conversion one of those listed in the above brands. You can:

  • Throw away std::string literal , for example, "You cannot devide by zero!"s (note the suffix s ).
  • Catch a const char* .
+4
source

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


All Articles