Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Defining appropriate Exception Handling classes

Posted on 2003-12-01
4
Medium Priority
?
267 Views
Last Modified: 2010-08-05
Hello,

I'd like to define exception classes that would be thrown/caught when necessary.

The example exception1 class below would be thrown when an integer entered is out of range.
Question1) Could the class be changed so that it can contain more meaningful information? e.g the value entered.

The exception "message" will then be passed back (using a return statement) to the calling function where the appropriate catch statement will reside.
i.e. the calling function will check the return status of the called function and check for the existence of "Error"  in the first 5 chars.
The info can then be used for diagnostics.

Question 2.) Also, Any possible problems with this approach?


-------------------------------------------
class  Exception1 {
public:
          Exception1()
            : message ("Error: Invalid value entered") {}
        const char *what() const { return message;}
    private:
        const char *message;
    };

-------------------------------------------

N.B. As this is really 2 questions, I've assigned 150 points to each one giving a total of 300 points...

Thanks for your help,


WesLondon
0
Comment
Question by:weslondon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9851475
Wes

It all depends in what info you want to store of the exception.

The Exception class can have any info you want and any methods you like. There are no restrictions about it.

Regarding number 2, I would suggest that you store a string instead a char * and maybe add a constructor that takes the message to be used.

class  Exception1 {
public:
         Exception1()
           : message ("Error: Invalid value entered") {}
         Exception1( const string & msg ) : message( msg ) {}
           : message ("Error: Invalid value entered") {}
       const char *what() const { return message.c_str();}
   private:
       string message;
   };

Hope this helps

Tincho
0
 
LVL 86

Accepted Solution

by:
jkr earned 900 total points
ID: 9851497
>>Question1) Could the class be changed so that it can contain more meaningful information?

Sure, e.g.

class  Exception1 {
public:
         Exception1()
           : message ("Error: Invalid value entered"), nInvalidValue(0) {}

         Exception1(int nVal)
           : message ("Error: Invalid value entered"), nInvalidValue(nVal) {}
       const char *what() const { return message;}
   private:
       const char *message;
       const int nInvalidValue;
   };

>>Question 2.) Also, Any possible problems with this approach?

No. But, why don't you use the 'regular' C++ mechanism of throwing and catching exceptions, e.g.

int enter_value () {

   // ... code ...

   // error condition
   if ( value > limit) {

      throw new Exception1 ( value);
  }
}

void foo () {

int n;

   try {

    n = enter_value();

  } catch ( Exception1* p) {

    // do diagnostics

    delete p;
  }
}
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 9852223
Exception1() : message ("Error: Invalid value entered") {}

I don't think that this is a good idea.  Really you should be doing this:

Exception1::Exception1()
{
   char[] default = "Error: Invalid value entered";

   message = new char[strlen("Error: Invalid value entered") + 1];
   strcpy(message, default);
}

Then you can be sure that the memory is allocated correctly.  Then you will also have to lose the 'const' before the declaration of message.

You will also need to rewrite the destructor hence to include the line:

delete[] message;

HTH
0
 
LVL 1

Expert Comment

by:travd
ID: 9854605
Sounds like homework to me!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question