question on try/catch

Discussion in 'C++' started by xuatla, Mar 5, 2005.

  1. xuatla

    xuatla Guest

    Hi,

    I tried the following code about try/catch (from cplusplus.com)

    // exceptions
    #include <ciostream>
    using namespace std;

    int main () {
    char myarray[10];
    try
    {
    for (int n=0; n<=10; n++)
    {
    if (n>9) throw "Out of range";
    myarray[n]='z';
    }
    }
    catch (char * str)
    {
    cout << "Exception: " << str << endl;
    }
    return 0;
    }

    It's expected to give "Exception: Out of range".
    but i complied in g++ 3.3.3. and run it, the output is "Aborted".

    Anyone help me to explain what's wrong here? or normal?

    Thanks a lot!

    xuatla
     
    xuatla, Mar 5, 2005
    #1
    1. Advertising

  2. xuatla

    Phil Staite Guest

    xuatla wrote:
    > Hi,
    >
    > I tried the following code about try/catch (from cplusplus.com)
    >
    > // exceptions
    > #include <ciostream>
    > using namespace std;
    >
    > int main () {
    > char myarray[10];
    > try
    > {
    > for (int n=0; n<=10; n++)
    > {
    > if (n>9) throw "Out of range";
    > myarray[n]='z';
    > }
    > }
    > catch (char * str)
    > {
    > cout << "Exception: " << str << endl;
    > }
    > return 0;
    > }
    >
    > It's expected to give "Exception: Out of range".
    > but i complied in g++ 3.3.3. and run it, the output is "Aborted".
    >
    > Anyone help me to explain what's wrong here? or normal?


    Try making the catch:

    catch( const char* str )

    I don't use g++ much, maybe it considers literal strings such as "Out of
    range" to be const char*. Therefore it wouldn't match/catch a
    non-const...
     
    Phil Staite, Mar 5, 2005
    #2
    1. Advertising

  3. xuatla

    modemer Guest

    "xuatla" <> ??????:d0b4rc$ho5$...
    > Hi,
    >
    > I tried the following code about try/catch (from cplusplus.com)
    >
    > // exceptions
    > #include <ciostream>
    > using namespace std;
    >
    > int main () {
    > char myarray[10];
    > try
    > {
    > for (int n=0; n<=10; n++)
    > {
    > if (n>9) throw "Out of range";
    > myarray[n]='z';
    > }
    > }
    > catch (char * str)


    should be:
    catch (char const *str)

    > {
    > cout << "Exception: " << str << endl;
    > }
    > return 0;
    > }
    >
    > It's expected to give "Exception: Out of range".
    > but i complied in g++ 3.3.3. and run it, the output is "Aborted".
    >
    > Anyone help me to explain what's wrong here? or normal?
    >
    > Thanks a lot!
    >
    > xuatla
     
    modemer, Mar 5, 2005
    #3
  4. xuatla

    Rolf Magnus Guest

    xuatla wrote:

    > Hi,
    >
    > I tried the following code about try/catch (from cplusplus.com)
    >
    > // exceptions
    > #include <ciostream>
    > using namespace std;
    >
    > int main () {
    > char myarray[10];
    > try
    > {
    > for (int n=0; n<=10; n++)
    > {
    > if (n>9) throw "Out of range";
    > myarray[n]='z';
    > }
    > }
    > catch (char * str)
    > {
    > cout << "Exception: " << str << endl;
    > }
    > return 0;
    > }
    >
    > It's expected to give "Exception: Out of range".
    > but i complied in g++ 3.3.3. and run it, the output is "Aborted".
    >
    > Anyone help me to explain what's wrong here? or normal?


    You're not catching the exception you threw. "Out of range" is of type
    const char[13], which gets converted to const char* before being thrown. But
    you're not catching const char*, and therefore the exception gets out. The
    result is correct (std::abort() being called).
     
    Rolf Magnus, Mar 5, 2005
    #4
  5. "xuatla" <> wrote in message
    news:d0b4rc$ho5$...

    > #include <ciostream>
    > using namespace std;
    >
    > int main () {
    > char myarray[10];
    > try
    > {
    > for (int n=0; n<=10; n++)
    > {
    > if (n>9) throw "Out of range";
    > myarray[n]='z';
    > }
    > }
    > catch (char * str)
    > {
    > cout << "Exception: " << str << endl;
    > }
    > return 0;
    > }
    >
    > It's expected to give "Exception: Out of range".
    > but i complied in g++ 3.3.3. and run it, the output is "Aborted".
    >
    > Anyone help me to explain what's wrong here? or normal?


    What happens if you change

    catch (char * str)

    to

    catch (const char * str)

    ?
     
    Andrew Koenig, Mar 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    369
    Kevin Spencer
    Aug 7, 2003
  2. bienwell
    Replies:
    4
    Views:
    3,860
    bienwell
    May 27, 2005
  3. Matt
    Replies:
    1
    Views:
    410
    Michael Rauscher
    Jun 12, 2004
  4. Matt
    Replies:
    5
    Views:
    423
    Adam Maass
    Jun 28, 2004
  5. Mark

    question about try/catch

    Mark, Oct 14, 2006, in forum: Java
    Replies:
    7
    Views:
    410
Loading...

Share This Page