By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

Obtain error message?

P: n/a
Hello. Consider the following code snippet (pseudo-code).

void foo()
{
/* do stuff */

if fatal error occurs
{
throw "error message";
}
}
try
{
foo();
}
catch(/* what should go here if I want to display the error message from the
throw statement above? */)
{
/* display error message */
}

What do I need to do to if I want to obtain the error message from the
throw-statement? I tried catching std::exception but it didn't catch the
exception. Do I need to derive a new class from std::exception if I want to
display the error messages to the user of the program? That's really all I
need exceptions for right now. If a fatal error occur, throw exception to
break call chain and display error message to the user then exit program
(while logging some debugging information). So do I derive my own exception
type (which would be very simple indeed) or do I use some built-in type or
do I do something completely different?

Thanks

/ William Payne
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"William Payne" <mi******************@student.liu.se> wrote in message news:bs**********@news.island.liu.se...
throw "error message";
You are throwing type const char*.
catch(/* what should go here if I want to display the error message from the
throw statement above? */)
{
/* display error message */
}
catch(const char* message) { cerr << message << endl; }
I tried catching std::exception but it didn't catch the
exception.
Because you didn't throw a std::exception (or something derived from it).
Do I need to derive a new class from std::exception if I want to
display the error messages to the user of the program?


No, but it might not be a bad idea.

Jul 22 '05 #2

P: n/a

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"William Payne" <mi******************@student.liu.se> wrote in message

news:bs**********@news.island.liu.se...
throw "error message";

You are throwing type const char*.
catch(/* what should go here if I want to display the error message from the throw statement above? */)
{
/* display error message */
}


catch(const char* message) { cerr << message << endl; }
I tried catching std::exception but it didn't catch the
exception.


Because you didn't throw a std::exception (or something derived from it).
Do I need to derive a new class from std::exception if I want to
display the error messages to the user of the program?


No, but it might not be a bad idea.


Thanks for the swift reply, Ron. I didn't realise that I was throwing a
const char* object, but now I know. The reason for using exceptions in my
program (I am trying to write a small text editor) is to break call chains
in a nice way and display information about the error. The errors are of the
unrecoverable sort and I am using it mainly for debugging purposes. I will
throw const char* objects for now and maybe derive my own exception class
later and incorporate logging of debug information so it's easier for me to
track down causes of exceptions. Again, thanks for the swift and very useful
reply.

/ William Payne
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.