LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2009, 04:28 PM   #1
grishaoks
LQ Newbie
 
Registered: Dec 2008
Posts: 7

Rep: Reputation: 0
Post Creating new exception


Hello,

I have a c++ class:

Code:
#ifndef WRONGPARAMETEREXCEPTION_H_

#define WRONGPARAMETEREXCEPTION_H_



#include <iostream>

#include <exception>

#include <string>

using namespace std;



#pragma once



class WrongParameterException: public exception

{

	public:

		WrongParameterException(char* message): exception(message) {};

		virtual ~WrongParameterException() throw() {};

}; 



#endif
when I try to compile it, the compiler gives me this error:

WrongParameterException.h: In constructor ‘WrongParameterException::WrongParameterException(char*)’:
WrongParameterException.h:14: error: no matching function for call to ‘std::exception::exception(char*&)’
/usr/include/c++/4.3/exception:59: note: candidates are: std::exception::exception()
/usr/include/c++/4.3/exception:57: note: std::exception::exception(const std::exception&)

Can anyone tell me what am I doing wrong? I tried changing the message variable to "string" or "const string" or "const string&" but it didnt help..

Here is how I use the new exception that I created from main:

Code:
try

	{

		if ((strToInt1 == -1) || (parameters[1] == NULL) || (strToInt3 == -1) || (parameters[3] != NULL))

		{

			throw WrongParameterException("Error in the config or commands file");

		}

	}

	catch(WrongParameterException e)

	{

		log.addMsg(e.what());

	}
Thanks in advance,

Greg
 
Old 02-22-2009, 11:31 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I think the basic problem is that you're passing "char *" (i.e. your parameter "char * message") instead of "exception &".

Theselinks might help:

http://www.cplusplus.com/reference/s...exception.html
http://www.cplusplus.com/doc/tutorial/exceptions.html

Last edited by paulsm4; 02-22-2009 at 11:33 PM.
 
Old 02-23-2009, 12:52 AM   #3
grishaoks
LQ Newbie
 
Registered: Dec 2008
Posts: 7

Original Poster
Rep: Reputation: 0
The thing is that I want it to pass a certain message and not a sort of exception so how can I do it without passing it a string or a char*?
 
Old 02-23-2009, 08:26 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
The thing is, if you say "WrongParameterException" is of type "exception" ... and you need to supply the right constructor for type "exception"!

Of course, there's nothing that says you need to subclass the standard C++ "exception". You don't even need "WrongParameterException" at all. You could simply do this (for example):
Quote:
throw new std::string ("Error in the config or commands file");
But frankly, it's probably cleaner to use standard exceptions (like you started out) ... and simply use them correctly.

IMHO .. PSM

PS:
Personally, I much prefer Java or C# exceptions over ANSI C++...

Last edited by paulsm4; 02-23-2009 at 08:28 AM.
 
Old 02-23-2009, 10:25 AM   #5
Biddle
Member
 
Registered: Jan 2009
Posts: 37

Rep: Reputation: 17
Simply derive from a runtime_error.
As for throwing a std::string this is very bad advice for a couple of reasons.
1) std::string can throw a bad_alloc exception.
2) you catch based on exception types not messages.
 
Old 02-23-2009, 10:39 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Even though the header file for <exception> exists, I believe that it is more appropriate to include <stdexcept>. The latter includes <exception>.

Also, never specify a "using namespace" in a header file.

Here's a working solution to your initial query:
Code:
#include <stdexcept>


class WrongParameterException : public std::exception
{
  public:
    WrongParameterException(const char* message) : m_msg(message) {}

    virtual ~WrongParameterException() throw() {}

    virtual const char* what() const throw() {return m_msg;}

  private:
    const char* m_msg;
};


// test
//
#include <iostream>

int main()
{
  try
  {
    throw WrongParameterException("test");
  }
  catch (WrongParameterException& wpe)
  {
    std::cerr << "Exception: " << wpe.what() << std::endl;
  }
}
Note that std::exception does not have an attribute to store a message. Think of it more as an "interface", but with a public constructor that accepts no args.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception creating UserDatabase MBeans for UserDatabase Luckshmi Linux - Newbie 0 12-21-2007 03:52 AM
Useradd not creating home directory when creating newuser meneedham Linux - Newbie 4 10-05-2007 12:11 PM
crontab exception 151803 Linux - Newbie 1 06-05-2007 07:41 AM
help createing exception class from base STL exception qwijibow Programming 4 04-20-2005 05:23 AM
Runtime Exception vs. Exception mikeshn Programming 1 09-22-2002 05:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration