. Advertisement .
..3..
. Advertisement .
..4..
I am working on cpp, but I found the following warning message:
Error : Terminate called after throwing an instance of 'char const*'
Is there any way to stabilize the issue “terminate called after throwing an instance of ‘char const*’”?
I read a lot of topics about this, but all of them were trying to install anything. Is this the correct way, or any recommendation for me?
Please find the beginning command below:
#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;
}
A string literal does not correspond to A handler is a match for an exception object of type E if:
Both of these were not applicable in the case of literal
This causes the exception to be uncaught and
It is best to throw an exception type that is not part of a hierarchy. The type name should communicate the error. If a handler or a group of handlers is needed, this will make it easier to handle the error in more robust ways.
If you do not want to use the standard practice, the conversion must be done according to the bullets. You can either:
std::string
. The former is better than the latter. A std::string
can be made from a string literal but it will not happen in a catch phrase. [except.handle]/3 explains the conversions that are permitted in catch clauses.
that can be converted to T by one or more of
std::string
conversion.
std::terminate
to run, as it is expected to with uncaught exceptions.
"You cannot devide by zero!"s
(note s
suffix).
const char*
.
The cause: You’re attempting to catch a
std::string
, but you make aconst char*
throw instead of throwing astd::string
. An uncaught exception causes your program to be terminated.Despite the fact that a string literal can be used to create a std::string, this will not occur in a catch clause.
Solution:
Neither of these was true in the case of converting a literal to a
std::string
.This causes the exception to be uncaught, and the run-time to use
std::terminate
, as is expected when an exception is uncaught.It’s preferable to throw a dedicated exception type so that the type name itself communicates the issue. If a handler (or a collection of handlers) is required, this will allow for more robust error handling.
You have two options if you don’t want to follow the regular procedure:
std::string
literal, such as"You cannot devide by zero!"s
(notice thes
suffix).const char*
.