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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5989727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:08:35+00:00 2026-05-22T23:08:35+00:00

For the following program: int DivZero(int, int, int); int main() { try { cout

  • 0

For the following program:

int DivZero(int, int, int);

int main()
{
    try {
        cout << DivZero(1,0,2) << endl;
    }
    catch(char* e)
    {
        cout << "Exception is thrown!" << endl;
        cout << e << endl;
        return 1;
    }
    return 0;
}

int DivZero(int a, int b, int c)
{
    if( a <= 0 || b <= 0 || c <= 0)
        throw "All parameters must be greater than 0.";
    return b/c + a;
}

Using char* e will give

terminate called after throwing an
instance of ‘char const*’

According to C++ Exception Handling , the solution is to use const char* instead.

Further reading from function (const char *) vs. function (char *) said that

The type of “String” is char*', not
const char*’

(this is a C discussion I think…)

Additional reading on Stack Overflow char* vs const char* as a parameter tells me the difference. But none of them address my questions:

  1. It seems like both char* and string* have limit on the numbers of characters. Am I correct?
  2. How does adding the keyword const to char* eliminates that limit? I thought the only purpose of const is to set a flag that said “unmodifiable”. I understand that const char* e means ” the pointer which points to unmodifiable char type”.

The solution to that error is to use const char* e.

Even const string* e doesn’t work. (just for the sake of testing…)

Can anyone explain, please? Thank you!

By the way, I am on Ubuntu, compiled by GCC, on Eclipse.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:08:35+00:00Added an answer on May 22, 2026 at 11:08 pm

    Why are you throwing and catching strings anyway?

    You should throw and catch exceptions, e.g. std::runtime_error

    The answer to your question is that whenever you insert a string in quotes in the code it returns a null terminated const char*

    The reason your code doesn’t work as above is because it’s the wrong type, so that catch, isn’t catching what you’re throwing. You’re throwing a const char*.

    There is no limit to the number of characters in a char array beyond the size of your stack/heap. If you’re referring to the example you posted, that person had created a fixed size array, so they were limited.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following simple program: int main() { char* v = getenv(TEST_VAR); cout
So I have the following program: int main(){ char* one = computer; char two[]
I have the following program: int main(int argc, char *argv[]) { int a, b;
I wrote following program int main () { char a=0xf; a=a+1; printf(%c\n,a); } the
I have the following program: ~/test> cat test.cc int main() { int i =
Consider the following C++ program struct str { int mem; str() try :mem(0) {
Does the following program invoke Undefined Behaviour in C? int main() { printf(Printf asking:
I've the following program: #include <stdio.h> int main() { int ch; while( ch =
The following C program: int doStuff(int afm[]); int main(){ int afm1[9] = {1,2,3,4,5,6,7,8,9}; //size=9
I wrote the following program using VS2008: #include <fstream> int main() { std::wofstream fout(myfile);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.