(Very) Basic question re: exception

  • Thread starter Michael Satterwhite
  • Start date
M

Michael Satterwhite

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
 
C

Claudio Puviani

Michael Satterwhite said:
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
 
M

Mike Wahler

Michael Satterwhite said:
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
 
M

Michael Satterwhite

Mike said:
catch (const char *s) {


:)

AAAAAAAAAAARRRRRRRRRGGGGGGGGHHHHHHHHH

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

Thanks much!
 
J

John Carson

Michael Satterwhite said:
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.
 
M

Michiel Salters

[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
 
J

John Carson

Michiel Salters said:
[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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top