Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Terminate called after throwing an instance of 'char const*' - simple way to solve it
Next
Answered
Camille O'Connor
  • 12
Camille O'Connor
Asked: May 17, 20222022-05-17T14:13:16+00:00 2022-05-17T14:13:16+00:00In: cpp

Terminate called after throwing an instance of ‘char const*’ – simple way to solve it

  • 12

. 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;
 }

♦ Suggestions of ITtutoria Team

A string literal does not correspond to 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.

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), or
  • the handler is of type cv T or cv T& and T is an unambiguous public base class of E, or
  • 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.

Both of these were not applicable in the case of literal std::string conversion.

This causes the exception to be uncaught and std::terminate to run, as it is expected to with uncaught exceptions.

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:

  1. Toss a HTMLTag11 literal like "You cannot devide by zero!"s (note s suffix).
  2. Get a const char*.
  • 1 1 Answer
  • 81 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-07T02:29:58+00:00Added an answer on June 7, 2022 at 2:29 am

    The cause: You’re attempting to catch astd::string, but you make a const char* throw instead of throwing a std::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:

    1. Use a std::string literal, such as "You cannot devide by zero!"s  (notice the s suffix).
    2. Attempt to catch a const char*.
    • 9
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.