473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

custom C++ exceptions

Hello everyone,

Recently I was coding a little app at home on my Linux box. I'm
using gcc 3.3.4 (well actually g++) to compile my programs. I was
compiling a module of the app into object code and got a nasty error
that gcc could not find a matching constructor. Here is the offening
code in simplified terms.
---code----

//file: myexception.hh
#include <exception>
#include <string>

using namespace std;

class MyException : public exception {
public:
MyException(const string & Message = "") :
exception(Message.c_str()) {}
};

//End of file.

---code---

And simply put this is what I would like to do with it.

---code---

#include <iostream>
#include <myexception.hh>

//And whatever other headers I may use.

void blah() { throw MyException("Blah, not done yet!"); }

int main() {

//Some code here...

try {
blah()
}
catch (MyException e) {

cout << e.what() << endl;
}

//and so on...
}

This seems to work with the HP-UX compiler and seems to be valid
code in most of the books I have read, however, upon inspection of the
header file on Linux, it does indeed lack a constructor for
std::exception::exception(const char *)

However, the GNU header seems to have a public function member
called what(). This leads me to believe that somewhere it has a
private data member that holds a message of some sort.

This leads me to my question: If the GNU c++ library lacks a
constructor to set a data member to hold some sort of message, then how
does one set a message at all.

Thank you all in advance,
Justin

Jul 23 '05 #1
4 4848
slack_justyb wrote:
Hello everyone,

Recently I was coding a little app at home on my Linux box. I'm
using gcc 3.3.4 (well actually g++) to compile my programs. I was
compiling a module of the app into object code and got a nasty error
that gcc could not find a matching constructor. Here is the offening
code in simplified terms.
---code----

//file: myexception.hh
#include <exception>
#include <string>

using namespace std;

class MyException : public exception {
public:
MyException(const string & Message = "") :
exception(Message.c_str()) {}
};

//End of file.

---code---

And simply put this is what I would like to do with it.

---code---

#include <iostream>
#include <myexception.hh>

//And whatever other headers I may use.

void blah() { throw MyException("Blah, not done yet!"); }

int main() {

//Some code here...

try {
blah()
}
catch (MyException e) {

cout << e.what() << endl;
}

//and so on...
}

This seems to work with the HP-UX compiler and seems to be valid
code in most of the books I have read, however, upon inspection of the
header file on Linux, it does indeed lack a constructor for
std::exception::exception(const char *)

However, the GNU header seems to have a public function member
called what(). This leads me to believe that somewhere it has a
private data member that holds a message of some sort.

This leads me to my question: If the GNU c++ library lacks a
constructor to set a data member to hold some sort of message, then how
does one set a message at all.

Thank you all in advance,
Justin


Here's a snip from the docs for 'std::exception'

<snip>

exception

class exception {
public:
exception() throw();
exception(const exception& rhs) throw();
exception& operator=(const exception& rhs) throw();
virtual ~exception() throw();
virtual const char *what() const throw();
};

The class serves as the base class for all exceptions thrown by certain
expressions and by the Standard C++ library. The C string value returned
by what() is left unspecified by the default constructor, but may be
defined by the constructors for certain derived classes. None of the
member functions throw any exceptions.

</snip>

As you can see, there is not a constructor for 'exception' that
takes a 'const char *' arg.

what() is virtual, derived class can define a what() that returns
meaningful data -and- implement a constructor that takes a
'const char *' arg.

Something like this might be appropriate:

class MyException : public exception
{
private:
std::string msg;

public:
MyException() throw() {}
~MyException() throw() {}
MyException(const MyException& rhs) throw() :
msg(rhs.msg) {}
MyException(const char * Message) : msg(Message) {}
MyException(const string & Message) : msg(Message) {}
MyException& operator=(const MyException& rhs) throw()
{ msg = rhs.msg; }
const char * what() const throw()
{ return msg.c_str(); }
};

Larry
Jul 23 '05 #2
Thank you.

I reread the header and noticed the virtual nature of the header.
I'll move on to futher implement my exception class in order to make it
more usable.

Cheers,
Justin

Jul 23 '05 #3
Ian
slack_justyb wrote:


catch (MyException e) {

cout << e.what() << endl;
}

Not answering your question, but never catch by value. You will both
slice any derived exception and copy it.

Catch by const reference.

Ian
Jul 23 '05 #4
Thank you for your information.
I was just trying to write some quick code to show my point in my
example above, but to be more correct I will show the modified code
here for any who may read this thread.

---code---

catch (const MyException &e) {

cout << e.what() << endl;
}

---code---

It was a slight mistake that I simply overlooked in writing my example.
I hope that this clears things up.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Dunaway | last post by:
I'm creating a Web service and a Windows Forms application to consume it. My question is about throwing a custom exception inside the WebService. Can that be done, and can the custom web service be...
8
by: Shawn Berg | last post by:
I have created a class with the following constructor: Public Sub New(ByVal ID As Integer) 'Use ID to get info from data store and set all properties End Sub I need to somehow handle an...
6
by: Flare | last post by:
Hi i just read: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp Wich is interesting reading by the way. But. I have'nt used exception very much to...
2
by: Shanthi | last post by:
I am using property grid in C# application. Property grid displays the propeties properly. But I have some problem in displaying custom validation message. I have integer type property. When I...
2
by: Matt | last post by:
Hello all, The app we are working on uses custom errors extensively to provide friendly error pages to users whilst logging the actual exceptions behind the scenes. However.... We are now...
15
by: bill salkin | last post by:
I'd like to create a custom error handler like this in VB.NET: .... try ... Throw ("Lender Name not in table") .... catch ("Lender Name not in table")
1
by: Vinoth Kumar | last post by:
Hi All, I have a problem in throwing custom exceptions from a webservice method. The custom exception is being converted into soapexception in the webservice client. only the custom exception...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
2
by: pillappa | last post by:
Hi, I am hitting this error consistently and don't know why it's happening. I would like to define all exceptions for my project in one file and use them across the project. Here's a sample - ...
3
by: Venkat | last post by:
Hi, I am working on an application (developed using c#2.0) which needs to do a big job and when you start the job in a single thread it takes long time to complete. So, we want to break the job...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.