Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Catching std::strings and c-style strings at once

Reply
Thread Tools

Catching std::strings and c-style strings at once

 
 
Kurt Krueckeberg
Guest
Posts: n/a
 
      11-17-2004

"Ney André de Mello Zunino" <> wrote in message
news:...
> Hello.
>
> Given the following try...catch construct:
>
> try
> {
> }
> catch (const string& msg)
> {
> }
>
> I might be overlooking something basic, but why can't the compiler use one
> of std::string's constructors in order to match:
>
> throw "Something happened!";
>
> The intent is to avoid having to add an extra catch block, i.e.:
>
> try
> {
> }
> catch (const string& msg)
> {
> }
> catch (const char* const msg)
> {
> }
>
> Thank you,


It is a bad practice to throw literals. It leads to code like
catch(const char *text) {
string str(text);
if (text == "error1") //...
else if (text == "error2") //...
else if (text == "error3")//...

}


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-17-2004
"Ney André de Mello Zunino" <> wrote...
> Given the following try...catch construct:
>
> try
> {
> }
> catch (const string& msg)
> {
> }
>
> I might be overlooking something basic, but why can't the compiler use one
> of std::string's constructors in order to match:
>
> throw "Something happened!";
>
> The intent is to avoid having to add an extra catch block, i.e.:
>
> try
> {
> }
> catch (const string& msg)
> {
> }
> catch (const char* const msg)
> {
> }


Matching between the type of the object thrown and the handler
is done in such way that user-defined conversions are not allowed.

class A {
public:
A(int);
};

int main() {
try {
throw 5;
}
catch (A) {
}
}

should produce "unhandled exception".

V


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
Guest
Posts: n/a
 
      11-17-2004
Hello.

Given the following try...catch construct:

try
{
}
catch (const string& msg)
{
}

I might be overlooking something basic, but why can't the compiler use
one of std::string's constructors in order to match:

throw "Something happened!";

The intent is to avoid having to add an extra catch block, i.e.:

try
{
}
catch (const string& msg)
{
}
catch (const char* const msg)
{
}

Thank you,

--
Ney André de Mello Zunino
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
catching empty strings (I guess that's what they are) brad Python 3 07-10-2007 04:59 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Regex to match say char 't' exactly once in a string and no more than once Gancy Perl Misc 4 02-03-2005 02:05 PM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57