(Very) Basic question re: exception

Discussion in 'C++' started by Michael Satterwhite, Apr 21, 2004.

  1. I haven't programmed C++ in years, so please forgive this very simple
    question.

    In function A i have:

    void A(int l) {
    if(l == 0)
    throw "This is a simple exception";
    ...

    In function B I have

    try {
    A(0);
    }
    catch (char * s) {
    <do something>
    }
    catch (...) {
    <do something else>
    }

    The general exception handler receives control every time (FWIW, I'm using
    gcc in Linux). If I remove the general exception handler, the program
    aborts.

    Question: Why isn't the (char *s) handler being activated. I know I'm
    missing the obvious.

    Thanks in advance
     
    Michael Satterwhite, Apr 21, 2004
    #1
    1. Advertising

  2. "Michael Satterwhite" <satterwh.X$NO$S$> wrote
    > I haven't programmed C++ in years, so please forgive this very simple
    > question.
    >
    > In function A i have:
    >
    > void A(int l) {
    > if(l == 0)
    > throw "This is a simple exception";
    > ...
    >
    > In function B I have
    >
    > try {
    > A(0);
    > }
    > catch (char * s) {
    > <do something>
    > }
    > catch (...) {
    > <do something else>
    > }
    >
    > The general exception handler receives control every time (FWIW, I'm using
    > gcc in Linux). If I remove the general exception handler, the program
    > aborts.
    >
    > Question: Why isn't the (char *s) handler being activated. I know I'm
    > missing the obvious.
    >
    > Thanks in advance


    Because 'char *' handlers can't catch 'const char *' or 'const char [N]' types.

    Claudio Puviani
     
    Claudio Puviani, Apr 21, 2004
    #2
    1. Advertising

  3. Michael Satterwhite

    Mike Wahler Guest

    "Michael Satterwhite" <satterwh.X$NO$S$> wrote in message
    news:vdihc.5686$...
    > I haven't programmed C++ in years, so please forgive this very simple
    > question.
    >
    > In function A i have:
    >
    > void A(int l) {
    > if(l == 0)
    > throw "This is a simple exception";
    > ...
    >
    > In function B I have
    >
    > try {
    > A(0);
    > }
    > catch (char * s) {


    catch (const char *s) {

    > <do something>
    > }
    > catch (...) {
    > <do something else>
    > }
    >
    > The general exception handler receives control every time (FWIW, I'm using
    > gcc in Linux). If I remove the general exception handler, the program
    > aborts.
    >
    > Question: Why isn't the (char *s) handler being activated. I know I'm
    > missing the obvious.


    :)

    -Mike
     
    Mike Wahler, Apr 21, 2004
    #3
  4. Mike Wahler wrote:

    >
    > "Michael Satterwhite" <satterwh.X$NO$S$> wrote in message
    > news:vdihc.5686$...
    >> I haven't programmed C++ in years, so please forgive this very simple
    >> question.
    >>
    >> In function A i have:
    >>
    >> void A(int l) {
    >> if(l == 0)
    >> throw "This is a simple exception";
    >> ...
    >>
    >> In function B I have
    >>
    >> try {
    >> A(0);
    >> }
    >> catch (char * s) {

    >
    > catch (const char *s) {
    >
    >> <do something>
    >> }
    >> catch (...) {
    >> <do something else>
    >> }
    >>
    >> The general exception handler receives control every time (FWIW, I'm
    >> using gcc in Linux). If I remove the general exception handler, the
    >> program aborts.
    >>
    >> Question: Why isn't the (char *s) handler being activated. I know I'm
    >> missing the obvious.

    >
    > :)


    AAAAAAAAAAARRRRRRRRRGGGGGGGGHHHHHHHHH

    Bad case of tunnel vision. The subtle I usually see, the obvious, never.

    Thanks much!
     
    Michael Satterwhite, Apr 21, 2004
    #4
  5. Michael Satterwhite

    John Carson Guest

    "Michael Satterwhite" <satterwh.X$NO$S$> wrote in
    message news:6rjhc.2277$
    > Mike Wahler wrote:
    >
    >>
    >> "Michael Satterwhite" <satterwh.X$NO$S$> wrote in
    >> message news:vdihc.5686$...
    >>> I haven't programmed C++ in years, so please forgive this very
    >>> simple question.
    >>>
    >>> In function A i have:
    >>>
    >>> void A(int l) {
    >>> if(l == 0)
    >>> throw "This is a simple exception";
    >>> ...
    >>>
    >>> In function B I have
    >>>
    >>> try {
    >>> A(0);
    >>> }
    >>> catch (char * s) {

    >>
    >> catch (const char *s) {
    >>
    >>> <do something>
    >>> }
    >>> catch (...) {
    >>> <do something else>
    >>> }
    >>>
    >>> The general exception handler receives control every time (FWIW, I'm
    >>> using gcc in Linux). If I remove the general exception handler, the
    >>> program aborts.
    >>>
    >>> Question: Why isn't the (char *s) handler being activated. I know
    >>> I'm missing the obvious.

    >>
    >> :)

    >
    > AAAAAAAAAAARRRRRRRRRGGGGGGGGHHHHHHHHH
    >
    > Bad case of tunnel vision. The subtle I usually see, the obvious,
    > never.
    >
    > Thanks much!




    I don't think it is obvious at all. Given a function taking a pointer to
    const char, you are allowed to pass it a pointer to non-const char. It is
    not "obvious" why it should be different with a catch.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Apr 21, 2004
    #5
  6. "John Carson" <> wrote in message news:<4085d92c$>...

    [SNIP]
    > Given a function taking a pointer to
    > const char, you are allowed to pass it a pointer to non-const char. It is
    > not "obvious" why it should be different with a catch.


    You're right, and it isn't different.
    Nor is there a difference for the reverse conversion: You can't pass
    a char const* to a function taking char*, nor can you pass a char const*
    exception to a handler catching char*.

    E.g.

    try { foo() }
    catch( char* e ) { if(e) e[0]=\0; }

    should be legal, as e is writeable. If foo throws "X", the handler
    should be skipped as it would modify the "X" literal.

    Regards,
    Michiel Salters
     
    Michiel Salters, Apr 21, 2004
    #6
  7. Michael Satterwhite

    John Carson Guest

    "Michiel Salters" <> wrote in message
    news:
    > "John Carson" <> wrote in message
    > news:<4085d92c$>...
    >
    > [SNIP]
    >> Given a function taking a pointer to
    >> const char, you are allowed to pass it a pointer to non-const char.
    >> It is not "obvious" why it should be different with a catch.

    >
    > You're right, and it isn't different.
    > Nor is there a difference for the reverse conversion: You can't pass
    > a char const* to a function taking char*, nor can you pass a char
    > const*
    > exception to a handler catching char*.
    >
    > E.g.
    >
    > try { foo() }
    > catch( char* e ) { if(e) e[0]=\0; }
    >
    > should be legal, as e is writeable. If foo throws "X", the handler
    > should be skipped as it would modify the "X" literal.
    >
    > Regards,
    > Michiel Salters



    Thanks. I clearly got confused about which direction of conversion we were
    discussing.

    Starting the discussion again from scratch, it does appear that the catch
    and function argument treatments of string literals are not the same. The
    following compiles on Comeau online:

    #include <iostream>

    void print(char *ptr)
    {
    std::cout << ptr << '\n';
    }

    int main ()
    {
    print("String Literal");
    return 0;
    }

    Thus a string literal is accepted as a char *. (It isn't if it is assigned
    to a pointer to const char and you try to use that pointer as the function
    argument.)

    Accordingly, it seems that the rule that string literals are pointers to
    const char is strictly enforced for catches but not for function arguments
    (presumably because of a C-compatibility issue with functions that is not
    relevant for catches).

    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Apr 21, 2004
    #7
    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. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    499
    Raymond Hettinger
    Jul 27, 2003
  2. aghazalp

    very very basic question

    aghazalp, Apr 2, 2006, in forum: Python
    Replies:
    6
    Views:
    351
    aghazalp
    Apr 2, 2006
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,680
    Merrill & Michele
    Oct 19, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    526
    Dave Thompson
    Feb 14, 2005
  5. mdh

    A very **very** basic question

    mdh, Sep 25, 2008, in forum: C Programming
    Replies:
    57
    Views:
    1,254
    August Karlstrom
    Sep 26, 2008
Loading...

Share This Page