* throw <const string>
@ 2002-11-17 21:43 thomas joseph
2002-11-18 1:16 ` Miguel Ramírez
0 siblings, 1 reply; 4+ messages in thread
From: thomas joseph @ 2002-11-17 21:43 UTC (permalink / raw)
To: gcc-help
Hi All,
I am trying a sample with c++ exception handling.
Could you tell me the point in the following code.
#include <iostream>
using namespace std;
int main()
{
char *buf = "Memory allocation failed!";
try
{
throw "Memory allocation failure!";
// throw buf;
}
catch( char *str)
{
cout << "Exception raised: " << str << '\n';
}
return 0;
}
If I compile this code and execute it gives me
segmentation fault. But If I comment the throw "Memory
..." and uncomment throw buf it works fine.
What I can n't understand is both buf and Memory
Allocation are pointers. (point to read only data.).
Why is it failing in the first case and passing in
second case ?
(If I compile with -fwritable-strins both the
scenarios are fine.).
Thanks in advance,
--thomas
http://careers.yahoo.com.au - Yahoo! Careers
- 1,000's of jobs waiting online for you!
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: throw <const string>
2002-11-17 21:43 throw <const string> thomas joseph
@ 2002-11-18 1:16 ` Miguel Ramírez
2002-11-18 1:29 ` Miguel Ramírez
0 siblings, 1 reply; 4+ messages in thread
From: Miguel Ramírez @ 2002-11-18 1:16 UTC (permalink / raw)
To: thomas joseph, gcc-help
Hi,
>
> Hi All,
> I am trying a sample with c++ exception handling.
> Could you tell me the point in the following code.
>
> #include <iostream>
> using namespace std;
> int main()
> {
> char *buf = "Memory allocation failed!";
>
> try
> {
> throw "Memory allocation failure!";
> // throw buf;
> }
> catch( char *str)
> {
> cout << "Exception raised: " << str << '\n';
> }
> return 0;
> }
>
C++ exceptions are objects:
#include <exception>
class MyCustomException : public std::exception
{
public:
const char* what()
{
return " Memory Allocation failure";
}
};
so you can now
#include <iostream>
#include "MyCustomException.hxx"
using namespace std;
int main()
{
try
{
throw "Memory allocation failure!";
// throw buf;
}
catch( std::exception& e )
{
cout << "Exception raised: " << e.what( ) << '\n';
}
return 0;
}
Note that there are already many exceptions classes available in the
standard library,
so this example is kind of "rash". Well, it may be quite a pain in the back
to take account
of the standard exception class hierarchy, but it may pay out in the long
term.
Miguel RamÃrez.
PS: Note that new already launches an exception when memory allocation fails
unless
you use the std::no_throw variant.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: throw <const string>
2002-11-18 1:16 ` Miguel Ramírez
@ 2002-11-18 1:29 ` Miguel Ramírez
0 siblings, 0 replies; 4+ messages in thread
From: Miguel Ramírez @ 2002-11-18 1:29 UTC (permalink / raw)
To: thomas joseph, gcc-help
Sorry,
> throw "Memory allocation failure!";
should read
throw MyCustomException();
Too much cut & paste :(
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: throw <const string>
@ 2002-11-18 3:43 Moore, Mathew L
0 siblings, 0 replies; 4+ messages in thread
From: Moore, Mathew L @ 2002-11-18 3:43 UTC (permalink / raw)
To: 'thomas joseph'; +Cc: gcc-help
> char* buf = "Memory allocation failure!";
> try
> {
> throw "Memory allocation failure!";
> // throw buf;
> }
> catch( char *str)
> {
> cout << "Exception raised: " << str << '\n';
> }
A string literal in a throw statement is not converted to a char*. For the
|throw "Memory allocation failure";|, you have to |catch(const char*)| for
this to work properly. On the other hand, your |buf| variable is a char*,
because it is declared that way. Hence, the |catch(char*)| works just fine
in the |throw buf;| case.
>
> If I compile this code and execute it gives me
> segmentation fault.
I guess I am also a little surprised it gives a seg fault.
>
>
> What I can n't understand is both buf and Memory
> Allocation are pointers. (point to read only data.).
|buf| is not read only. You have to declare it that way.
const char* buf = "Memory allocation failure!";
You may want to consider using the standard library exceptions.
--Matt
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2002-11-18 11:43 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-17 21:43 throw <const string> thomas joseph
2002-11-18 1:16 ` Miguel Ramírez
2002-11-18 1:29 ` Miguel Ramírez
2002-11-18 3:43 Moore, Mathew L
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).