Why can't throw/catch a const string

This is a discussion on Why can't throw/catch a const string within the C++ Programming forums, part of the General Programming Boards category; Hi friends, I found the following codes and the catch block couldn't get the string correctly. Could you please help ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28

    Question Why can't throw/catch a const string

    Hi friends,
    I found the following codes and the catch
    block couldn't get the string correctly. Could you please help to see it. Thanks.
    Code:
    #include <iostream>
    using namespace std;
    void test(void)
    {
    	try
    	{
    		cout<<"This is try block!\n";
    		throw "String";
    //		char str[]="String";
    //		throw str;
    		cout<<"This is hidden block!\n";
    	}
    	catch(char *str)
    	{
    		cout<<"This is catch block!"<<endl;
    		cout<<"The caught string is "<<str<<endl;
    	}
    }
    int main(void)
    {
    	cout<<"Start"<<endl;
    	test();
    	cout<<"End"<<endl;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What happens when you attempt to catch a const char* (which is what "String" is)?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28
    The line
    Code:
    throw "String";
    should occur some errors.
    I got a debug error when running the code:
    Project XX raised exception class const char * with message 'Exception Object Address:0x8F444E' in CB2007.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If your debugger is set to pop up a message when any exception occurs, then you will get that message even if it will be caught. The question is whether the catch block is entered. Can you continue when the message comes up? Or run it but not through the debugger.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28
    I also tried the following code to catch the exception but I found the exception type is unkonwn
    and not be "char *"
    Code:
    #include <iostream>
    using namespace std;
    void test(void)
    {
    	try
    	{
    		cout<<"This is try block!\n";
    		throw "String";
    //		char str[]="String";
    //		throw str;
    		cout<<"This is hidden block!\n";
    	}
    	catch(char *str)
    	{
    		cout<<"This is catch block!"<<endl;
    		cout<<"The caught string is "<<str<<endl;
    	}
    	catch(...)
    	{
    		cout<<"This is public catch block!"<<endl;
    	}
    }
    int main(void)
    {
    	cout<<"Start"<<endl;
    	test();
    	cout<<"End"<<endl;
    }
    /*output
    Start
    This is try block!
    This is public catch block!
    End
    */

  6. #6
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28
    I run the program in windows console and the "Debug error" pops up.

    If touch on "Ignore" the following is the output:
    /*output
    Start
    This is try block!

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
    */
    Last edited by chenayang; 03-19-2008 at 11:50 PM.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by chenayang View Post
    I also tried the following code to catch the exception but I found the exception type is unkonwn
    The exception type is not unknown. Daved just told you what it is.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28
    Hi Daved and brewbuck,
    Thank you very much.
    Got it.
    Code:
    catch(const char *str)
    .
    But I'm still confused about when I need to add the "const".
    Could you please show me more about how to use the "const" especially when it is needed?

    For example if
    Code:
    throw 'C';
    for catch block the const is not necessarily need.
    Code:
    	catch(char str)
    //          catch(const char str) is also OK
    Last edited by chenayang; 03-20-2008 at 12:09 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    A string literal such as "My string" is const. Simply because it is treated as such by the language and is typically stored in some read-only memory.
    You can't catch long with int, either, so type matters.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why on earth would anyone want to throw a literal string instead of an object anyways?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21